The 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.
Timers module has the following functions:
Scheduling Timers: It is used to call a function after a set period of time.
Cancelling Timers: It is used to cancel the scheduled timer.
1. setImmediate() method: It schedules the “immediate” execution of the callback after I/O events callbacks. In the following example, multiple setImmediate functions are called. Whenever we do these callback functions are queued for execution in the order in which they are created. The entire callback queue is processed after every event loop iteration. If an immediate timer is queued from inside an executing callback, that timer will not be triggered until the next event loop iteration.
Started... 1 3 2 4
In the above example, on the first iteration of event loop function A is fired. Then on second iteration function B is fired, and C is fired on the third iteration. Similarly functions D and E are fired on the fourth and fifth iteration respectively.
2. setInterval() method: It repeats the execution of the callback after every t time in milliseconds passed as a parameter.
Executed before A... Hello World! Hello World! Hello World! Hello World! Hello World! ...
3. setTimeout() method: It schedules the execution of the callback after a certain time in milliseconds which is passed as a parameter.
Executed before A... Hello World!
4. clearImmediate() method: It is used to simply cancel the Immediate object created by setImmediate() method.
5. clearInterval() method: It is used to cancel the Immediate object created by setInterval() method.
The clearInterval() clears the setInterval ‘si’ after 500 ms, then function A is executed four times.
Hello World! Hello World! Hello World! Hello World!
6. clearTimeout() method: It is used to cancel the Immediate object created by setTimeout() method.
Only setInterval ‘si2’ is executed as ‘si1’ is cleared by clearTimeout() method.
- Nodejs | Automatic restart NodeJs server with nodemon
- Nodejs - Connect MongoDB with Node app using MongooseJS
- Cowsay in Nodejs using Requests library
- Nodejs | Web Crawling using Cheerio
- Web-Socket in NodeJS
- Signup Form Using Nodejs and MongoDB
- Dockerizing a simple Nodejs app
- Nodejs | DNS
- Encrypting Data in NodeJS
- Difference between NodeJS and AngularJS
- Nodejs | Jimp
- Nodejs – Connect Mysql with Node app
- Nodejs | DNS | setServers()
- NodeJS | Building simple REST API in express
- NodeJs - Handling invalid routes
- Nodejs | authentication using Passportjs and passport-local-mongoose
- Nodejs | GM charcoal() Function
- Nodejs | GM blur() Function
- Nodejs | GM sharpen() Function
- Nodejs | GM drawLine() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.