How to clear all intervals with JavaScript?

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.