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.

More JavaScript tutorials

See all