Node.js Tutorial

Node.js Timer

Node.js Timer

Node.js Timer Functions

In Node.js, timer functions are global ÔÇö no import required. They schedule code to run after a delay, at intervals, or right after the current event loop phase. Mastering them is essential for asynchronous Node.js work.

Node.js Tutorial:-
Complete Python Course:-

Set Timer Functions

  • setTimeout(callback, ms) ÔÇö run once after a delay.
  • setInterval(callback, ms) ÔÇö run repeatedly at intervals.
  • setImmediate(callback) ÔÇö run after the current I/O phase.

Clear Timer Functions

  • clearTimeout(id) ÔÇö cancel a setTimeout.
  • clearInterval(id) ÔÇö stop a setInterval.
  • clearImmediate(id) ÔÇö cancel a setImmediate.

setInterval Example

// Print every 1 second
setInterval(() => {
  console.log("Hey! 1 second completed!");
}, 1000);

setTimeout Example

setTimeout(() => {
  console.log("Runs once after 1 second");
}, 1000);

Recursive setTimeout (Cleaner than setInterval)

function recursive() {
  console.log("Tick!");
  setTimeout(recursive, 1000);
}
recursive();

Cancelling Timers

function welcome() {
  console.log("Welcome to UpdateGadh!");
}

const id1 = setTimeout(welcome, 1000);
const id2 = setInterval(welcome, 1000);

clearTimeout(id1);  // cancel the timeout
clearInterval(id2); // stop the interval

setTimeout vs setImmediate

setImmediate runs after the current poll phase, while setTimeout(fn, 0) waits for at least one tick ÔÇö useful for splitting heavy work.

Download New Real Time Projects:- Click here
Complete Advance AI topics:-

Conclusion

Timers in Node.js are global and easy to use. setTimeout, setInterval, and setImmediate cover almost every scheduling need. For more tutorials, stay tuned to .

node js setinterval
nodejs settimeout
nodejs timer example
node:timers/promises
nodejs.timer typescript
setimmediate vs settimeout
node js sleep
node js timer w3schools

Source Code Available

Interested in This Project?

Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
Chat on WhatsApp

We usually reply within a few minutes

Leave a Reply

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

Chat with us