The clearImmediate
function is used to clear the function call scheduled by the setImmediate
function. Both these functions are found in the Timers
module of Node.js.
clearInterval(timerIdentifier);
The function takes in one parameter, i.e., timeIdentifier
. This is a Timeout
object which is returned by SetImmediate
.
console.log("Before the setImmediate call")let timerID = setImmediate(() => {console.log("Hello, World")});console.log("After the setImmediate call")clearImmediate(timerID);
In the above example, on line 2, an arrow function has been scheduled using the setImmediate
function. On line 4, this scheduling was revoked by the clearImmediate
function.
function myFunction(platform){console.log("Hi, Welcome to " + platform);}console.log("Before the setImmediate call")let timerID = setImmediate(myFunction, "Educative");console.log("After the setImmediate call")for(let i=0; i<10; i++){console.log("Iteration of loop: "+i);}clearImmediate(timerID)
In the above example, the function call is scheduled by setImmediate
in line 6. This scheduling was revoked by the clearImmediate
function call in line 11. Therefore, the function myFunction
is never executed.
RELATED TAGS
CONTRIBUTOR