嗨,伙计们,当我的计时器变为 0 时,我如何将计时器重置为 10 秒,下面是我的代码。当我在连接中声明时,我当前代码中发生的事情不会重新启动为零。
app.js :
countdown = 10;
var onInterval = function() {
countdown--;
if(countdown == 0){
var endTime = currTimer();
clearInterval(myInterval);
io.sockets.emit('timer', { timer: countdown });
};
var myInterval = setInterval(onInterval, 1000);
io.sockets.on('connection', function (socket){
socket.on('restart timer', function (data) {
setTimeout(function(){
countdown = 10;
myInterval = setInterval(onInterval, 1000);
}
}
}