Node.js Timers Complete Reference
Node.js Timers module in Node.js contains various functions that allow us to execute a block of code or a function after a set period of time. The Timers module is global, we do not need to use require() to import it.
Example:
Javascript
// Node.js program to demonstrate the// Immediate Class methods // Setting Immediate by setImmediate Methodvar Immediate = setImmediate(function alfa() { console.log("0.>",12);}); // Printing Immediate.hasRef methodconsole.log("1.>",Immediate.hasRef());// Returns true // Printing Immediate.ref before unrefconsole.log("2.>",Immediate.ref());// Returns timer reference // Printing Immediate.unref methodconsole.log("3.>",Immediate.unref());// Returns Immediate reference and// sets hasRef to false // Printing Immediate.hasRef before unrefconsole.log("4.>",Immediate.hasRef());// Returns false // Clears setInterval ImmediateclearImmediate(Immediate); // Prints after clearing Immediateconsole.log("5.>",2); |
Output:
1.> true
2.> Immediate {
_idleNext: null, ……, [Symbol(triggerId)]: 1
}
3.> Immediate {
_idleNext: null, ……, [Symbol(triggerId)]: 1
}
4.> false
5.> 2The Complete List of Timers are listed below:
Node.js Timers Class | Description |
|---|---|
| Immediate Timer Class | Immediate Class has an object (setImmediate()) which is created internally to scheduled actions, and (clearImmediate()) can be passed in order to cancel those scheduled actions. |
| Timeout Timer Class | Timeout Class has an object (setTimeout()/setInterval()) which is created internally for scheduled actions, and (clearTimeout()/clearInterval()) can be passed in order to cancel those scheduled actions. |



Please Login to comment...