Trusted answers to developer questions

What is clearInterval() in Node.js?

Get the Learn to Code Starter Pack

Break into tech with the logic & computer science skills you’d learn in a bootcamp or university — at a fraction of the cost. Educative's hand-on curriculum is perfect for new learners hoping to launch a career.

The clearInterval function is used to clear the repeated function calls scheduled by the setInterval function. Both of these functions are found in the Timers module of Node.js.

Parameters

clearInterval(timerIdentifier);

In the above snippet of code, timerIdentifier is a Timer object returned by the setInterval function.

Examples

console.log("Before the setInterval call")
let timerID = setInterval(() => {console.log("Half of a second has passed")}, 500);
console.log("After the setInterval call")
clearInterval(timerID)

In the above snippet of code, the setInterval function scheduled the execution of the arrow function passed as a parameter after every half of a second, but it was unscheduled by the clearInterval function call with the accurate identifier.

function myFunction(platform){
console.log("Hi, Welcome to " + platform);
}
console.log("Before the setInterval call")
let timerID = setInterval(myFunction, 2000, "Educative");
console.log("After the setInterval call")
clearInterval(timerID);

In the above example, it is evident that the clearInterval function call on line 9 unscheduled the repeated function calls scheduled inside the setInterval function on line 6.

RELATED TAGS

node.js

CONTRIBUTOR

Amaaz Ahmad
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?