How do I stop a window.setInterval in javascript?

There’s no built-in “pause” function, but you can stop it, and then start a new interval with the same function.

First, you need to capture the id returned by the call to setInterval:

let intervalId = window.setInterval(...);

Then when you want to stop it, call

 window.clearInterval(intervalId);

In your case I’d suggest defining setScreen by itself, and not inside of the call to setInterval. This way you can just use intervalId = window.setInterval(setScreen, 2000) when you want to resume it.

Leave a Comment