gpt4 book ai didi

Javascript - 我的 while 循环中的 setTimeOut 不起作用

转载 作者:行者123 更新时间:2023-11-28 20:38:54 25 4
gpt4 key购买 nike

我想做这样的事情:

<p id="sec">5</p>
<script>
var i = 5;
while (i > 0){
setTimeout(i--,1000);
document.getElementById("sec").innerHTML = i;
}
if (i === 0){
window.location = "index.php";
}
</script>

一瞬间,它就把我重定向到了index.php?为什么会发生这种情况?如何让它发挥作用?

最佳答案

var i = 5;
setTimeout(updateTime,1000);

function updateTime()
{
document.getElementById("sec").innerHTML = i--;

if (i === 0)
window.location = "index.php";
else
setTimeout(updateTime, 1000);
}

关于Javascript - 我的 while 循环中的 setTimeOut 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14772565/

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