gpt4 book ai didi

javascript - JS while循环中的setTimeout

转载 作者:行者123 更新时间:2023-11-30 16:38:08 26 4
gpt4 key购买 nike

因为 setTimeout 在 while 循环中崩溃。

我不知道是否有办法做到这一点,但我正在尝试制作一个。这是目前的样子。

<script>
var send = true;
function sendit()
{
alert("test");
return true;
}
while(true)
{
if(send == true)
{
send = false;
setTimeout(function(){
if(sendit() == true) {
send = true;
}
}, 5000);
}
}
</script>

这样可以吗?

最佳答案

您还没有解释您希望代码做什么。如果你想让它每 5 秒提醒一次“测试”,那么你需要这个:

<script>
function sendit()
{
alert("test");
// Call sendit() the next time, repeating
setTimeout(sendit, 5000);
}
// Call sendit() the first time
setTimeout(sendit, 5000);
</script>

不需要循环,只需要让函数重新调度自己。

关于javascript - JS while循环中的setTimeout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32401899/

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