Node.js Timers module

08 Oct, 2021
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.setImmediate()setInterval()setTimeout()Cancelling Timers: It is used to cancel the scheduled timer.clearImmediate()clearInterval()clearTimeout()
Scheduling Timers: It is used to call a function after a set period of time.setImmediate()setInterval()setTimeout()
setImmediate()
setInterval()
setTimeout()
Cancelling Timers: It is used to cancel the scheduled timer.clearImmediate()clearInterval()clearTimeout()
clearImmediate()
clearInterval()
clearTimeout()
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.
setImmediate(function A() {    setImmediate(function B() {      console.log(1);      setImmediate(function D() {         console.log(2);      });    });      setImmediate(function C() {      console.log(3);      setImmediate(function E() {         console.log(4);      });    });});  console.log('Started...');
Output:
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 after every 1000 milliseconds// from the start of the programsetInterval(function A() {    return console.log('Hello World!');}, 1000);  // Executed right awayconsole.log('Executed before A...');
Output:
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 after 3000 milliseconds // from the start of the programsetTimeout(function A() {    return console.log('Hello World!');}, 3000);  // executed right awayconsole.log('Executed before A...');
Output:
Executed before A...
Hello World!

4. clearImmediate() method: It is used to simply cancel the Immediate object created by setImmediate() method.
var si = setImmediate(function A() {    console.log(1);});  // clears setInterval siclearImmediate(si);  console.log(2);
Output:
2
5. clearInterval() method: It is used to cancel the Immediate object created by setInterval() method.
var si = setInterval(function A() {    return console.log("Hello World!");}, 500);  setTimeout(function() {    clearInterval(si);}, 2000);
The clearInterval() clears the setInterval ‘si’ after 500 ms, then function A is executed four times.
Output:
Hello World!
Hello World!
Hello World!
Hello World!

6. clearTimeout() method: It is used to cancel the Immediate object created by setTimeout() method.
// si1 is cleared by clearTimeout()var si1 = setTimeout(function A() {    return console.log("Hello World!");}, 3000);  // only si2 is executedvar si2 = setTimeout(function B() {    return console.log("Hello Geeks!");}, 3000);  clearTimeout(si1);
Only setInterval ‘si2’ is executed as ‘si1’ is cleared by clearTimeout() method.Output:
Hello Geeks!

Node.js-Basics
Picked
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.