Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript中重新启动SetInterval_Javascript_Node.js_Socket.io - Fatal编程技术网

在Javascript中重新启动SetInterval

在Javascript中重新启动SetInterval,javascript,node.js,socket.io,Javascript,Node.js,Socket.io,我很难在清除间隔后将时间重新启动到10秒。 当状态为“成功”时,应按设置的间隔重新运行 下面是我的代码: <code> var countdown = 10; var startTime = currTimer(); var myInterval = setInterval(function() { countdown--; if(countdown == 0){ var endTime = currTimer(); clearInterval(myInterval)

我很难在清除间隔后将时间重新启动到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);

最好的方法可能是将函数保存在一个变量中,您可以将该变量传递给
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);