In this article, we are going to explore timers in Node.js and how to use them in various scenarios. The timers method in node.js contains different types of functions that are used for executing a block of code or a function at a specific period of time. It is a global module that is not required to be imported.
We can divide the timers module into 2 types of functions:
- Scheduling Timers: These types of timers are used for calling a function after a specified time period.
- Cancelling Timers: These types of timers are used for cancelling the scheduled timers.
setImmeditate() method: This is a scheduling timer that schedules an immediate execution of the callback. These callback functions are queued as per the scheduled time and then executed. This event loop iteration is processed as per the entire callback queue. The timer will not be triggered when any immediate timer is queued from an executing callback. This is only 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 Timer Execution' );
|
Output:
Started...
1
3
2
4
setInterval() method: This method repeats the execution of the callback that is called after every t milliseconds.
Javascript
setInterval( function intervalTimer() {
return console.log( 'Hey Geek! Welcome to Tutorials Point' )
}, 1000);
console.log( 'This is executed before intervalTimer() method.' );
|
Output:
This is executed before intervalTimer() method.
Hey Geek! Welcome to Tutorials Point
Hey Geek! Welcome to Tutorials Point
Hey Geek! Welcome to Tutorials Point
...
setTimeout() method: This method is used for scheduling the execution of the callback that is called after a certain time in milliseconds. This timeout is passed as a parameter in the method.
Javascript
setTimeout( function setTimeout() {
return console.log( 'Hello Geek! Welcome to GFG' );
}, 3000);
console.log( 'Executed before setTimeout...' );
|
Output:
Executed before A...
Hello Geek! Welcome to GFG
clearImmediate() method: This method is used for simply cancelling the scheduled timer that was created using the setImmediate() scheduling method.
Javascript
const timer = setImmediate( function A() {
console.log( "Timer is set" );
});
clearImmediate(timer);
console.log( "Timer is cleared" );
|
Output:
Timer is cleared
clearInterval() method: This method is used for cancelling the timer method that was scheduled using the setInterval() method.
Javascript
const timer = setInterval( function A() {
return console.log( "Hello Geek!" );
}, 500);
setTimeout( function () {
clearInterval(timer);
console.log( "Interval Timer cleared" )
}, 2000);
|
Output:
Hello Geek!
Hello Geek!
Hello Geek!
Interval Timer cleared
clearTimeout() method: This method is used for clearing the scheduled timer that was set using the setTimeout() method.
Javascript
const si1 = setTimeout( function A() {
return console.log( "Hello World!" );
}, 3000);
const si2 = setTimeout( function B() {
return console.log( "Hello Geeks!" );
}, 3000);
clearTimeout(si1);
|
Output:
Hello Geeks!