Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

node.js

What is clearInterval() in Node.js?

Amaaz Ahmad

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 ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring