Node.js: 运行 setInterval while certain variable is true?

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

我有一个 setInterval 函数和 isRunning 变量。 如何在 isRunning 设置为 false 时停止 setInterval?

谢谢。

查看内联评论:

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);