如何清除匿名 setInterval 函数(如下所示),以便 i 停止递增?
var i = 0;
setInterval(function(){
console.log(i);
i++;
}, 1000);
最佳答案
您需要将其存储在变量中,然后在 clearInterval
中传递该变量。
var i = 0;
let variable = setInterval(function(){
i++;
console.log(i)
}, 1000);
//to clear interval after 5 seconds to check if it works
setTimeout(() => clearInterval(variable),5000)
关于javascript - 清除匿名 setInterval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54598559/