What are the various timing features of Node.js ?

20 May, 2020
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 awayconsole.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 intervalsetInterval(function () {    console.log('setInterval() function running');}, 5000);  // An immediate, its callback will be // executed before those defined abovesetImmediate(function () {    console.log('setImmediate() function running');});  // IO callbacks and code in the normal// event loop runs before the timersconsole.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.
Node.js-Misc
Picked
Node.js
Web Technologies
Web technologies Questions
Write From Home
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Node.js fs.writeFile() Method
How to install the previous version of node.js and npm ?
Difference between promise and async await in Node.js
Mongoose | findByIdAndUpdate() Function
JWT Authentication with Node.js
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?
Differences between Functional Components and Class Components in React