JS,JQ實現暫停FOR循環,間隔幾秒後再繼續執行
阿新 • • 發佈:2017-06-21
doctype on() 執行 apps string min html fun count
<!DOCTYPE html>
<head>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js">
</script>
<script>
function testing(){
var string = ‘1u2,1u3,1u4,1u5‘;
var count = 0;
for (var i = 0; i < string.length; i++) {
if (string[i] == ‘,‘) {
count++;
}
}
mytimer = setInterval(function(){test();}, 2000); //利用setInterval實現間隔兩秒執行一次test()函數
function test(){
var barcode=string.split(‘,‘)[count];
count--;
if(count<0){clearInterval(mytimer);} //clearInterval取消循環
alert(barcode);
}
}
</script>
</head>
<body>
<input type="button" style="margin-left:50%;margin-top:20%;" value="test" onclick="testing();" />
</body>
</html>
JS,JQ實現暫停FOR循環,間隔幾秒後再繼續執行