我想知道如果让循环每秒暂停一次,然后继续计算我拥有的“糖果”数量,该如何做呢?
编辑:我想暂停一下,以便使我所拥有的糖果量每秒增加1。当前显示开始显示1到10。我试图复制在这里完成的效果(我对JavaScript非常陌生): http://candies.aniwey.net/
//booleans
var count = new Boolean;
count = true;
//integers
var candy = 0;
//strings
var txt = "You have ";
var txt1 = " candies.";
var br = "<br>";
//loops
while (candy <= 10) {
if (candy == 0) {
document.write(txt + candy + txt1);
document.write(br);
candy++;
} else if (candy == 1) {
document.write(txt + candy + " candy.");
document.write(br);
candy++;
} else {
document.write(txt + candy + txt1);
document.write(br);
candy++;
}
}
最佳答案
代替循环,使用window.setInterval()
:
var candy = 0;
window.setInterval(function() {
document.write("You have " + ++candy + " candy.<br/>");
}, 1000);
关于javascript - JavaScript循环中的暂停,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22157880/