JavaScript clearInterval() method explained

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.

Take your skills to the next level ⚡️

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

No spam. Unsubscribe anytime.