The Wayback Machine - https://web.archive.org/web/20240118171511/https://www.geeksforgeeks.org/node-js-timers-complete-reference/
Open In App
Related Articles

Node.js Timers Complete Reference

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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 Method
const Immediate = setImmediate(function alfa() {
    console.log("0.>", 12);
});
 
// Printing Immediate.hasRef method
console.log("1.>", Immediate.hasRef());
// Returns true
 
// Printing Immediate.ref before unref
console.log("2.>", Immediate.ref());
// Returns timer reference
 
// Printing Immediate.unref method
console.log("3.>", Immediate.unref());
// Returns Immediate reference and
// sets hasRef to false
 
// Printing Immediate.hasRef before unref
console.log("4.>", Immediate.hasRef());
// Returns false
 
// Clears setInterval Immediate
clearImmediate(Immediate);
 
// Prints after clearing Immediate
console.log("5.>", 2);


Output:

1.> true
2.> Immediate {
  _idleNext: null, ……, [Symbol(triggerId)]: 1
}
3.> Immediate {
 _idleNext: null, ……, [Symbol(triggerId)]: 1
}
4.> false
5.> 2

The Complete List of Timers is 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.
Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 04 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials