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.
The timers module has the following functions:
Scheduling Timers: It is used to call a function after a set period of time.
Canceling Timers: It is used to cancel the scheduled timer.
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.
javascript
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 the event loop function A is fired. Then on the 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 iterations respectively.
setInterval() method: It repeats the execution of the callback after every t time in milliseconds passed as a parameter.
javascript
setInterval( function A() {
return console.log( 'Hello World!' );
}, 1000);
console.log( 'Executed before A...' );
|
Output:
Executed before A...
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
...
setTimeout() method: It schedules the execution of the callback after a certain time in milliseconds which is passed as a parameter.
javascript
setTimeout( function A() {
return console.log( 'Hello World!' );
}, 3000);
console.log( 'Executed before A...' );
|
Output:
Executed before A...
Hello World!
clearImmediate() method: It is used to simply cancel the Immediate object created by the setImmediate() method.
javascript
let si = setImmediate( function A() {
console.log(1);
});
clearImmediate(si);
console.log(2);
|
Output:
2
clearInterval() method: It is used to cancel the Immediate object created by the setInterval() method.
javascript
let 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!
clearTimeout() method: It is used to cancel the Immediate object created by the setTimeout() method.
javascript
let si1 = setTimeout( function A() {
return console.log( "Hello World!" );
}, 3000);
let si2 = setTimeout( function B() {
return console.log( "Hello Geeks!" );
}, 3000);
clearTimeout(si1);
|
Only setInterval ‘si2’ is executed as ‘si1’ is cleared by the clearTimeout() method.
Output:
Hello Geeks!