开发者

Stopping a Node.js callback by clearing setInterval

开发者 https://www.devze.com 2023-03-17 06:37 出处:网络
Need a call on this situation. If I have this function: var timerID; function playBack(client,log){ if (timerID) clearInterval(timerID);

Need a call on this situation. If I have this function:

var timerID;
function playBack(client,log){
    if (timerID) clearInterval(timerID);
    timerID = setInterval(function(){
        var buffer = [],
            numberOfLogLines = log.length;
        while(numberOfLogLines > 0){
            var l = log.pop().trim(); // pull from the top
            buffer.push(l);
            if(l.split(",")[0] === "$TIMETOSEND"){ //flag for the timming signal
                client.send("{\"playback\":" + JSON.stringify(buffer) + "}");
                break;
            }
        }
    },playBackSpeed);
}

and call

clearInterval(timerID)

at some other point does this stop the callback? I am trying to create a pause/play situation which to start the playBack again I would invoke the playBack(client,log) ag开发者_JAVA技巧ain to restart. Seems to work but, I am wondering does clearing the interval stop the callBack. I don't want to create a log jam for the case of the heavy "pause/play" happy people.


A call to clearInterval( timerID ) will stop any future calls to your anonymous function. If there are previous calls of it running, then these will finish.

0

精彩评论

暂无评论...
验证码 换一张
取 消