How to stop setInterval with JavaScript?

Spread the love

Sometimes, we want to stop setInterval with JavaScript.

In this article, we’ll look at how to stop setInterval with JavaScript.

How to stop setInterval with JavaScript?

To stop setInterval with JavaScript, we call clearInterval.

For instance, we write

let interval;
//...
interval = setInterval(updateDiv, 3000);
//...
clearInterval(interval);

to call setInterval with a callback and period to return a timer ID number.

And then we call clearInterval with the timer ID number to clear the timer.

Conclusion

To stop setInterval with JavaScript, we call clearInterval.

Leave a Reply

Your email address will not be published. Required fields are marked *