What are the various timing features of Node.js ?

The timer modules in Node.js consists of functions that help to control the timings of code execution. It includes setTimeout(), setImmediate(), and setInterval() methods.

1. setTimeout() Method: The setTimeout() method is used to schedule code execution after a designated amount of milliseconds. The specified function will be executed once. We can use the clearTimeout() method to prevent the function from running. The setTimeout() method returns the ID that can be used in clearTimeout() method.

Syntax:

setTimeout(callback, delay, args )

Parameters:

  • callback: This parameter holds the function that to be executed.
  • delay: This parameter holds the number of milliseconds to wait before calling the callback function.
  • args: This parameter holds the optional parameter.

Example:



filter_none

edit
close

play_arrow

link
brightness_4
code

let str = 'GeeksforGeeks!';
  
setTimeout(function () {
    return console.log(str);
}, 5000);
  
// This console log is executed right away
console.log('Executing setTimeout() method');

chevron_right


Output:

Executing setTimeout() method
GeeksforGeeks!

Notice that the console is executing first. The trick to realizing is that the code is only guaranteed to execute after at least that length of time has passed.

2. setImmediate() Method: The setImmediate() method is used to execute code at the end of the current event loop cycle. Any function passed as the setImmediate() argument is a callback that can be executed in the next iteration of the event loop.

Syntax:

setImmediate(callback, args)

Parameters:

  • callback: This parameter holds the function to call at the end of this turn of the Node.js Event Loop.
  • args: This parameter holds the optional arguments for the function.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

setTimeout(function () {
    console.log('setTimeout() function running');
}, 5000);
  
// An interval
setInterval(function () {
    console.log('setInterval() function running');
}, 5000);
  
// An immediate, its callback will be 
// executed before those defined above
setImmediate(function () {
    console.log('setImmediate() function running');
});
  
// IO callbacks and code in the normal
// event loop runs before the timers
console.log('Simple statement in the event loop');

chevron_right


Output:

Simple statement in the event loop
setImmediate() function running
setTimeout() function running
setInterval() function running
setInterval() function running
setInterval() function running
setInterval() function running
. . .

Notice, how even though setImmediate function is defined after setTimeout and setInterval functions, it runs ahead of them.

3. setInterval() Method: The setInterval() method is used to call a function at specified intervals (in milliseconds). It is used to execute the function only once after a specified period.
We can use the clearInterval() method to prevent the function from running. The setInterval() method returns the ID which can be used in clearInterval() method.

Syntax:

setInterval(callback, delay, args)

Parameters:

  • callback: This parameter holds the function that to be called when the timer elapse.
  • delay: This parameter holds the number of milliseconds to wait before calling the vallback function.
  • args: This parameter holds the optional arguments for the function.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

setInterval(function() {
    console.log('Welcome to GeeksforGeeks');
}, 5000);

chevron_right


Output:

Welcome to GeeksforGeeks
Welcome to GeeksforGeeks
.....

It will print output multiple times with a time interval of 5 seconds.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.