What is clearInterval() in Node.js?

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.

Copyright ©2024 Educative, Inc. All rights reserved