gpt4 book ai didi

javascript - JavaScript 循环期间暂停

转载 作者:行者123 更新时间:2023-11-28 08:28:18 27 4
gpt4 key购买 nike

我想知道如何让这个 if 循环每秒暂停一次,然后继续计算我拥有的“糖果”数量。

编辑:我想暂停,以便每秒我拥有的糖果数量增加 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com