Timers in Node.js | setTimeout, setInterval and setImmediate

Introduction:

Timers module provides a way for scheduling functions to be called after a given time. The Timer object is a global object in Node.js, therefore it’s not necessary to import it using the require() function call. All the methods are available globally to emulate the browser JavaScript API.

Timer functions:

  • setImmediate() —–> Executes a given function immediately.
  • setInterval() —–> Executes a given function at every given milliseconds
  • setTimeout() —–> Executes a given function after a given time (in milliseconds)
  • clearInterval() —–> Cancels an Interval object
  • clearTimeout() —–> Cancels a Timeout object
  • clearImmediate() —–> Cancels an Immediate object
  • unref() —–> Stops the Timeout object from remaining active.

setTimeout() fuction:

setTimeout() can be used to schedule code execution after a certain amount of milliseconds. setTimeout() takes two arguments:

  • The first argument is a function to execute.
  • The second argument is the delayed time in milliseconds.
function myFunction(arg) {
  console.log(`arg was => ${arg}`);
}

setTimeout(myFunction, 3000, 'foo');

The above function will execute after 3 seconds. setTimeout() returns a Timeout object that can be used to cancel the timeout.

setInterval() function:

If you want to schedule a task then setInterval() function is the best choice. With the help of setInterval() function you can execute a task in a predefined interval. setInterval() takes two arguments:

  • The first argument is a function that will run an infinite number of times.
  • The second argument is the delay interval in milliseconds.
function intervalFunction() {
  console.log('Hey! I am still running');
}

setInterval(intervalFunction, 5000);

The above function will execute about every 5 seconds. setInterval() returns a Timeout object that can be used to reference the timeout that was set. This returned object can be used to cancel the timeout same as setTimeout().

setImmediate() function:

setImmediate() is used to execute code at the end of the current event loop cycle. This code will execute after any I/O operations in the current event loop and before any timers scheduled for the next event loop. The first argument to setImmediate() will be the function to execute. Any subsequent arguments will be passed to the function when it is executed.

console.log('before immediate');

setImmediate((arg) => {
  console.log(`executing immediate: ${arg}`);
}, 'so immediate');

console.log('after immediate');

The above function passed to setImmediate() will execute after all runnable code has executed, and the console output will be:

before immediate
after immediate
executing immediate: so immediate

setImmediate() returns an Immediate object, which can be used to cancel the scheduled immediate.

Conclusion:

Here we have learned how to use javascript timers in Node.js. These timer functions execute asynchronously so it does not block the event loop. Create a tool with help of these timer functions and have fun. Thanks for reading.

#Happy Coding #TopCode #topcode.in

#node.js timers #timers in node.js #settimeout node.js #settimeout node.js example #setinterval node.js #setinterval node.js example #setimmediate node,js #differerence between setinterval and settimeout #What is difference between settimeout and setInterval #What is difference between settimeout and setInterval in nodejs settimeout nodejs await #settimeout nodejs.timeout #settimeout nodejs async #settimeout nodejs promise #settimeout nodejs not working #settimeout nodejs express #settimeout nodejs arguments #settimeout nodejs wait #settimeout nodejs example #nodejs settimeout alternative #nodejs settimeout api #node js settimeout accuracy #node js after settimeout #nodejs settimeout blocking #nodejs settimeout callback must be a function #nodejs settimeout cancel #nodejs settimeout cleartimeout #nodejs settimeout callback #nodejs settimeout closure #nodejs settimeout does not work #settimeout is not defined node js #nodejs settimeout exception #node js settimeout eventloop #nodejs settimeout execution #settimeout function nodejs #nodejs settimeout function with parameters #node js settimeout async function #nodejs http settimeout #node js http request settimeout #settimeout in nodejs #settimeout in nodejs example #nodejs settimeout in loop #node js settimeout in mongodb #settimeout in node js express #node settimeout is not defined #settimeout in node js w3schools #settimeout node js #settimeout node js example #settimeout node js not working #settimeout node js promise #settimeout nodejs ms #settimeout javascript node js #settimeout blocking node.js #js settimeout nodejs #settimeout node limit #nodejs settimeout loop #nodejs settimeout memory leak #nodejs settimeout milliseconds #node settimeout minimum #node settimeout milliseconds #nodejs mock settimeout #nodejs net settimeout #nodejs override settimeout #node js settimeout object #nodejs settimeout parameters #node js settimeout performance #nodejs settimeout promisify #settimeout node red #settimeout request nodejs #nodejs settimeout return #nodejs settimeout recursive #nodejs settimeout repeat #nodejs settimeout reject #node settimeout return #reset settimeout nodejs #settimeout node sleep #nodejs settimeout sync #settimeout and setinterval in node js #nodejs settimeout stop #nodejs settimeout sleep #node settimeout setinterval #settimeout vs setinterval nodejs #settimeout nodejs.timer #nodejs.settimeout typescript #nodejs settimeout then #settimeout trong nodejs #node js settimeout time #nodejs settimeout unref #nodejs settimeout vs setinterval #node settimeout vs setimmediate #nodejs settimeout with parameters #nodejs window.settimeout #nodejs websocket settimeout #nodejs settimeout 0 #settimeout 0 nodejs #settimeout in node setinterval nodejs example #setinterval nodejs.timeout #setinterval nodejs stop #setinterval nodejs.timer #setinterval node js bug #nodejs setinterval async #nodejs setinterval clearinterval #nodejs setinterval not working #setinterval node arguments #nodejs setinterval blocking #node setinterval bug #nodejs setinterval cancel #node js setinterval change interval #node js setinterval callback #nodejs change setinterval #node setinterval doesn’t work #nodejs setinterval 1 day #setinterval en node.js #nodejs setinterval clearinterval example #nodejs express setinterval #setinterval function nodejs #node setinterval function #node js setinterval async function #setinterval node js example #node js setinterval http request #setinterval in nodejs #nodejs setinterval start immediately #setinterval function in nodejs #using setinterval in node js #setinterval node js #setinterval javascript nodejs #typescript setinterval node js.timeout #setinterval function node js #setinterval remove node.js #js setinterval nodejs #nodejs setinterval loop #nodejs setinterval limit #nodejs setinterval memory leak #node setinterval multiple #node setinterval mysql #nodejs multiple setinterval #nodejs module setinterval #setinterval on node #nodejs setinterval promise #nodejs setinterval performance #nodejs setinterval parameters #nodejs setinterval polling #node setinterval pause #setinterval node red #nodejs setinterval redis #node setinterval run immediately #nodejs response setinterval #nodejs setinterval request #nodejs setinterval server #nodejs setinterval vs settimeout #settimeout and setinterval in node js #node js setinterval example #settimeout vs setinterval nodejs #typescript setinterval nodejs.timeout #nodejs setinterval typescript #typescript setinterval nodejs.timer #setinterval trong nodejs #nodejs setinterval unref #node setinterval vs cron #node setinterval variable #nodejs setinterval value #setinterval in node #settimeout setimmediate #setinterval in node js setinterval vs settimeout nodejs #setinterval nodejs.timeout #nodejs setinterval vs settimeout #settimeout and setinterval in node js #setinterval node js example #setinterval nodejs #settimeout nodejs #settimeout nodejs example #settimeout node js example #settimeout node js #setinterval nodejs example #settimeout setinterval setimmediate #setinterval settimeout javascript #settimeout setimmediate #setinterval in node #setinterval and settimeout #setinterval in node js

, , , , , ,

Related posts

Latest posts

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Please disable your adblocker or whitelist this site!

How to whitelist website on AdBlocker?

How to whitelist website on AdBlocker?

  1. 1 Click on the AdBlock Plus icon on the top right corner of your browser
  2. 2 Click on "Enabled on this site" from the AdBlock Plus option
  3. 3 Refresh the page and start browsing the site