Open In App

What are the various timing features of Node.js ?

Improve
Improve
Like Article
Like
Save
Share
Report

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:




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


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:




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');


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:




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


Output:

Welcome to GeeksforGeeks
Welcome to GeeksforGeeks
.....

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



Last Updated : 20 May, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads