在清除间隔后,我很难将时间重新启动到 10 秒。 当状态为成功时,它应该重新运行设置的间隔
下面是我的代码:
<code>
var countdown = 10;
var startTime = currTimer();
var myInterval = setInterval(function() {
countdown--;
if(countdown == 0){
var endTime = currTimer();
clearInterval(myInterval);
countdown = 10;
if (status == "success"){
setTimeout(function(){myInterval}, 200);
}
}
io.sockets.emit('timer', { countdown: countdown });
}, 2000);
</code>
最佳答案
最好的方法可能是将函数保存在一个可以传递给 setInterval
调用的变量中。这样它就变成了一条易于重复的线。
var countdown = 10;
var startTime = currTimer();
var onInterval = function() {
countdown--;
if(countdown == 0){
var endTime = currTimer();
clearInterval(myInterval);
countdown = 10;
if (status == "success"){
setTimeout(function(){
myInterval = setInterval(onInterval, 2000);
}, 200);
}
}
io.sockets.emit('timer', { countdown: countdown });
};
var myInterval = setInterval(onInterval, 2000);
关于javascript - 在 Javascript 中重新启动 SetInterval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19925618/