Node.js: running setInterval while certain variable is true?

时间:2015-06-26 09:51:10

标签: javascript node.js timeout setinterval

I have a setInterval function and isRunning variable. How do I stop setInterval when isRunning gets set to false?

Thanks.

1 个答案:

答案 0 :(得分:2)

See the comments inline:

var isRunning = true; // Define it outside so that can be accessed inside setInterval

var interval = setInterval(function() {
    // Your code here


    // Check if variable is false, then clear interval
    if (isRunning == false) {
        clearInterval(interval);
    }
}, 100);