我想知道如何讓循環每秒暫停一次,然後繼續計算「糖果」的數量。JavaScript中的循環暫停
編輯:我想暫停一下,以便每秒鐘糖果數量增加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++;
}
}
你看着用[的setInterval(https://developer.mozilla.org/en-US /docs/Web/API/Window.setInterval)? – tkone
你爲什麼要暫停? – Huangism