JavaScript clearInterval() method explained

Posted on Mar 17, 2021

Learn how clearInterval() method works here


The JavaScript clearInterval() method is a method from the DOM Window object that you can use to stop the function that you execute using the setInterval() method.

For example, the following fnLog() function that runs every 1 second under setInterval() method:

let timer = setInterval(fnLog, 1000);

function fnLog() {
  console.log("Function fnLog executed");
}

To stop the fnLog() function above from running forever, you call the clearInterval() method:

clearInterval(timer);

With that, the fnLog() function will stop executing.

Both the browser and Node.js has the clearInterval() method implemented, so you can use the method on both environments.

Level up your programming skills

I'm sending out an occasional email with the latest programming tutorials. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.