Node.js – Timers Module – Scheduling Timers

Node.jsServer Side ProgrammingProgramming

The timers module contains functions that can execute code after a certain period of time. You do not need to import the timer module explicitly, as it is already globally available across the emulated browser JavaScript API.

Timers module is mainly categorised into two categories

  • Scheduling Timers − This timer schedules the task to take place after a certain instant of time.

    • setImmediate()

    • setInterval()

    • setTimeout()

  • Cancelling Timers − This type of timers cancels the scheduled tasks which is set to take place.   

    • ClearImmediate()

    • clearInterval()

    • clearTimeout()

Scheduling Timers

1. setTimeout() Method

The setTimeout() method schedules the code execution after a designated number of milliseconds. Only after the timeout has occurred, the code will be executed. The specified function will be executed only once. This method returns an ID that can be used in clearTimeout() method.

Syntax

setTimeout(function, delay, [args])

Example 1

let str = 'TutorialsPoint!';

setTimeout(function () {
   // Will be printed after 2 seconds
   return console.log(str);
}, 2000);

// This will be printed immediately
console.log('Executing setTimeout() method');

Output

Executing setTimeout() method
TutorialsPoint!

2. setImmediate() Method

setImmediate() executes the code at the end of the current event loop cycle.

Syntax

setImmediate(function, [args])

Example

filename - immediate.js

// Setting timeout for the function
setTimeout(function () {
   console.log('setTimeout() function running');
}, 5000);

// Running this function immediately before any other
setImmediate(function () {
   console.log('setImmediate() function running');
});
// Directly printing the statement
console.log('Simple statement in the event loop');

Output

Simple statement in the event loop
setImmediate() function running
setTimeout() function running

3. setInterval() Method

setInterval() executes the code after the specified interval. The function is executed multiple times after the interval has passed. The function will keep on calling until the process is stopped externally or using code after specified time period.

Syntax

setInterval(function, delay, [args])

Example

filename - interval.js

setInterval(function() {
   console.log('Tutoirals Point - SIMPLY LEARNING !');
}, 1000);

Output

Tutoirals Point - SIMPLY LEARNING !
Tutoirals Point - SIMPLY LEARNING !
Tutoirals Point - SIMPLY LEARNING !
Tutoirals Point - SIMPLY LEARNING !
Tutoirals Point - SIMPLY LEARNING !
Tutoirals Point - SIMPLY LEARNING !
Tutoirals Point - SIMPLY LEARNING !
Tutoirals Point - SIMPLY LEARNING !
Tutoirals Point - SIMPLY LEARNING !

4.clearImmediate() Method

This method clears the Immediate timer object that is created by the setImmediate() method.

Syntax

clearImmediate(timer)

Example

filename - clearImmediate.js

// clearImmediate() Example

var timer = setImmediate(function A() {
   console.log("Timer set");
});

clearImmediate(timer);
console.log("Timer cancelled");

Output

Timer cancelled

5.clearInterval() Method

This method clears the Immediate timer object that is created by the setInterval() method.

Syntax

clearInterval(timer)

Example

filename - clearInterval.js

// clearInterval() Example

var si = setInterval(function A() {
   return console.log("Setting Intevals for 500 ms !");
}, 500);

// Cleared the interval from 1000 ms
setTimeout(function() {
   clearInterval(si);
}, 1000);

Output

Setting Intevals for 500 ms !

6.clearTimeout() Method

This method clears the Immediate timer object that is created by the setTimeout() method.

Syntax

clearTimeout(timerObject)

Example

filename - clearTimeout.js

// clearTimeout() Example

var timer = setTimeout(function A() {
   return console.log("Hello TutorialsPoint!");
}, 500);

// timer2 will be executed
var timer2 = setTimeout(function B() {
   return console.log("Welcome to TutorialsPoint!");
}, 500);
clearTimeout(timer);

Output

Welcome to TutorialsPoint!
raja
Published on 30-Sep-2021 09:45:13
Advertisements