Timers module in Node.js (original) (raw)

Last Updated : 25 Apr, 2026

The Timers module in Node.js allows execution of code after a specified delay or at repeated intervals. It is a global module, so it can be used without importing it.

Types of Timers

In Node.js, timers can be categorized into two types based on their purpose.

A. Scheduling Timers

Scheduling timers allow functions to execute after a delay or at repeated intervals. They help manage timed and asynchronous execution of code.

1. **setImmediate() method: schedules a callback to execute immediately after I/O events in the event loop. Callbacks run in the order they are created after each event loop iteration.

Node `

setImmediate(function A() { setImmediate(function B() { console.log(1); setImmediate(function D() { console.log(2); }); }); setImmediate(function C() { console.log(3); setImmediate(function E() { console.log(4); }); }); }); console.log('Started...');

`

2. **setInterval() method: repeatedly executes a callback function after every specified time interval (in milliseconds) until it is stopped using clearInterval().

Node `

// Executed after every 1000 milliseconds // from the start of the program setInterval(function A() { return console.log('Hello World!'); }, 1000); // Executed right away console.log('Executed before A...');

`

**Output

Executed before A...
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
...

3. **setTimeout() method: schedules the execution of a callback function once after a specified delay (in milliseconds).

Node `

// Executed after 3000 milliseconds // from the start of the program setTimeout(function A() { return console.log('Hello World!'); }, 3000); // executed right away console.log('Executed before A...');

`

**Output

Executed before A...
Hello World!

B. Canceling Timers

Canceling timers is used to stop or prevent the execution of previously scheduled timer functions. It helps control timer-based operations in Node.js applications.

1. **clearImmediate() method: cancels an Immediate object created by setImmediate(), preventing the scheduled callback from executing.

Node `

let si = setImmediate(function A() { console.log(1); }); // clears setInterval si clearImmediate(si); console.log(2);

`

2. **clearInterval() method: cancels the timer created by setInterval(), stopping the repeated execution of the callback function.

Node `

let si = setInterval(function A() { return console.log("Hello World!"); }, 500); setTimeout(function () { clearInterval(si); }, 2000);

`

**Output

Hello World!
Hello World!
Hello World!
Hello World!

3. **clearTimeout() method: cancels the timer created by setTimeout(), preventing the scheduled callback from executing.

Node `

// si1 is cleared by clearTimeout() let si1 = setTimeout(function A() { return console.log("Hello World!"); }, 3000); // only si2 is executed let si2 = setTimeout(function B() { return console.log("Hello Geeks!"); }, 3000); clearTimeout(si1);

`

**Output

Hello Geeks!