Categories
JavaScript Answers

How to clear all intervals with JavaScript?

Spread the love

Sometimes, we want to clear all intervals with JavaScript.

In this article, we’ll look at how to clear all intervals with JavaScript.

How to clear all intervals with JavaScript?

To clear all intervals with JavaScript, we call clearInterval on all timer IDs.

For instance, we write

const intervalId = window.setInterval(() => {}, Number.MAX_SAFE_INTEGER);

for (let i = 1; i < intervalId; i++) {
  window.clearInterval(i);
}

to call setInterval to return a timer ID number.

Then we use a for loop to loop from 1 to intervalId and call clearInterval on all ID i.

Conclusion

To clear all intervals with JavaScript, we call clearInterval on all timer IDs.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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