JavaScript clearInterval() method explained

Learn how clearInterval() method works here

Posted on March 17, 2021


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.

Related articles:

Grab the free JavaScript book today 👍

Learn the building blocks of JavaScript programming language like data types, functions, objects, arrays and classes.

Use the knowledge from the book to build a small but solid program.

Learn more