jquery中,for循环或者while循环中每执行一次,停顿一秒,在执行下一次循环
function sleep(numberMillis) {
var now = new Date();
var exitTime = now.getTime() + numberMillis;
while (true) {
now = new Date();
if (now.getTime() > exitTime)
return;
}
}
使用
sleep(1000);
用计时器模拟for
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.2.min.js"></script>
<div id="dv"></div>
<script>
var i = 0, max = 5, timer;
function countDown() {
$('#dv').html(i++);
if (i >= max) clearInterval(timer)
}
timer = setInterval(countDown, 1000);
countDown()
</script>