Sometimes, we want to reset the setInterval timer with JavaScript.
In this article, we’ll look at how to reset the setInterval timer with JavaScript.
How to reset the setInterval timer with JavaScript?
To reset the setInterval timer with JavaScript, we can use the clearInterval function.
For instance, we write
const myFn = () => {
console.log("idle");
};
let myTimer = setInterval(myFn, 4000);
//...
clearInterval(myTimer);
myTimer = setInterval(myFn, 4000);
to call setInterval with myFn to run the function every 4 seconds.
We assign the return timer ID number to myTimer.
Then we call clearInterval with myTimer to stop the timer.
Next, we call setInterval with the same arguments and then we assign the returned timer number to myTimer again.
Conclusion
To reset the setInterval timer with JavaScript, we can use the clearInterval function.