Describe the use of Timer methods in Node.js
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
// Initializing multiple setImmediate() timer setImmediate( function A() { setImmediate( function B() { console.log(1); setImmediate( function D() { console.log(2); }); }); // Initializing other setImmediate timers 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
// Method will be executed 1000ms after start setInterval( function intervalTimer() { return console.log( 'Hey Geek! Welcome to Tutorials Point' ) }, 1000); // Executed when the program starts 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
// Method will be executed 3 seconds // after the program starts setTimeout( function setTimeout() { return console.log( 'Hello Geek! Welcome to GFG' ); }, 3000); // Statement will be printed as soon as 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
// This method will clear the setImmediate() timer const timer = setImmediate( function A() { console.log( "Timer is set" ); }); // clearing the timer 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
// This method will clear the setInterval() timer // Setting the Interval timer const timer = setInterval( function A() { return console.log( "Hello Geek!" ); }, 500); // Clearing the interval timer 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
// This method will clear the setTimeout() timer // Setting the Timeout timer const si1 = setTimeout( function A() { return console.log( "Hello World!" ); }, 3000); // Only the below method is executed const si2 = setTimeout( function B() { return console.log( "Hello Beginner!" ); }, 3000); // Clearing timer 1 clearTimeout(si1); |
Output:
Hello Beginner!