Removing intervals
suggest changewindow.setInterval() returns an IntervalID, which can be used to stop that interval from continuing to run. To do this, store the return value of window.setInterval() in a variable and call clearInterval() with that variable as the only argument:
function waitFunc(){
console.log("This will be logged every 5 seconds");
}
const interval = window.setInterval(waitFunc,5000);
window.setTimeout(function(){
clearInterval(interval);
},32000);
setInterval is available in browsers and in Node. The delay is a minimum. With exact timers the callback runs at 5, 10, 15, 20, 25, and 30 seconds, six times, and the 32-second timeout stops it before another run. A delayed timer can change that count.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents