gpt4 book ai didi

javascript - 如何在 JavaScript 中进行 while 循环倒计时秒数。 (初学者)

转载 作者:行者123 更新时间:2023-12-03 08:12:06 25 4
gpt4 key购买 nike

我的代码遇到了一些问题。我似乎无法获得倒计时秒数的 while 循环。数字末尾必须有多少个零才能得到 1 秒?

    var Time = Math.floor((Math.random() * 1500000) + 500000); // trying to make this use seconds not whatever it uses /\   
console.log(Time / 100000);
//defining a random variable
while (Time >= 0) {
if (Time == 0) {
document.write("done");
}
Time--;
}

最佳答案

在循环内使用 cpu 周期逻辑来定义倒计时的秒数并不是一个好主意。

您可以使用setInterval函数,如下所示:

    var seconds = 10;
var timer = setInterval(function() {
seconds--;
if(seconds == 0) {
document.write("done");
clearInterval(timer);
} else {
document.write(seconds + " seconds left");
}
}, 1000);

关于javascript - 如何在 JavaScript 中进行 while 循环倒计时秒数。 (初学者),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34099947/

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