Open In App

Node.js Timeout Timer Class

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The timer module is used for scheduling functions which will be called at some future period of time. It is a global API So, there is no need to import (require(“timers”)) to use it.

Timeout Class has an object (setTimeout()/setInterval()) which is created internally to scheduled actions, and (clearTimeout()/clearInterval()) can be passed in order to cancel those scheduled actions. When a timeout is scheduled, By default, the event loop of Node.js will continue running until clearTimeout() is called. The setTimeout() method returns timeout objects that are used to control this default behavior, and exports both timeouts.ref() and timeout.unref() functions.

The four Timeout Class objects are defined below: 

timeout.hasRef(): (Added in v11.0.0) This timeout object keeps the event loop active until the returned ‘True’ if returned ‘false’ breaks the event loop.

Syntax:

timeout.hasRef()

Return Value <boolean>: This timeout object keeps the event loop active if returned ‘True’.

timeout.ref(): (Added in v9.7.0) When the Timeout is active and (timeout.ref()) called then it requests that the Node.js event loop did not exist for so long. Anyway, calling this Method multiple times does not have any effect.

Syntax:

timeout.ref()

Return Value <Timeout>: It returns a timeout reference.

timeout.refresh() (Added in v10.2.0): This method refreshes the timer without allocating a new JavaScript object. The start time of the timer is set to the current time, and the timer is rescheduled from the previously specified duration to the current time by calling its callback. By using timeout.refresh() on a timer which is already called its callback and then it reactivates the timer.

Syntax:

timeout.refresh()

Return Value <Timeout>: It returns the timeout reference. 

timeout.unref() (Added in v9.7.0) When the Timeout is active it does not require the Node.js event loop to remain active. The callback of the Timeout object is invoked after the process gets exit if any other activity keeps the event loop running. Anyway, calling this Method also multiple times does not have any effect.

Syntax:

timeout.unref()

Return Value <Timeout>: It returns the timeout reference.

Example: In this example, we will Node.js program to demonstrate the Timeout Class methods

Filename: index.js 

Javascript




// Node.js program to demonstrate the
// Timeout Class methods
 
// Setting Timeout by setTimeout Method
let Timeout = setTimeout(function alfa() {
    console.log("0.> Setting Timeout", 12);
});
 
// Printing Timeout.hasRef method
console.log("1 =>", Timeout.hasRef());
// returns true
 
// Printing Timeout.ref before unref
console.log("2 =>", Timeout.ref());
Timeout.unref()
Timeout.ref()
// Returns timer reference
 
// Printing Timeout.refresh before unref
console.log("3 =>", Timeout.refresh());
// Returns timer reference
 
// Printing Timeout.unref method
console.log("4 =>", Timeout.unref());
// Returns Timeout reference and sets
// hasRef to false
 
// Printing Timeout.hasRef before unref
console.log("5 =>", Timeout.hasRef());
// Returns false
 
// Clears setInterval Timeout
clearTimeout(Timeout);
// Prints after clearing Timeout
 
console.log("6 => Printing after clearing Timeout");


Run the index.js file using the following command:

node index.js

Output:

1 => true 
2 => Timeout { 
_idleTimeout: 1, 
_idlePrev: [TimersList], 
_idleNext: [TimersList], 
_idleStart: 52, 
_onTimeout: [Function: alfa], 
_timerArgs: undefined, 
_repeat: null, 
_destroyed: false, 
[Symbol(refed)]: true, 
[Symbol(asyncId)]: 2, 
[Symbol(triggerId)]: 1 
} 
3 => Timeout { 
_idleTimeout: 1, 
_idlePrev: [TimersList], 
_idleNext: [TimersList], 
_idleStart: 87, 
_onTimeout: [Function: alfa], 
_timerArgs: undefined, 
_repeat: null, 
_destroyed: false, 
[Symbol(refed)]: true, 
[Symbol(asyncId)]: 2, 
[Symbol(triggerId)]: 1 
} 
4 => Timeout { 
_idleTimeout: 1, 
_idlePrev: [TimersList], 
_idleNext: [TimersList], 
_idleStart: 87, 
_onTimeout: [Function: alfa], 
_timerArgs: undefined, 
_repeat: null, 
_destroyed: false, 
[Symbol(refed)]: false, 
[Symbol(asyncId)]: 2, 
[Symbol(triggerId)]: 1 
} 
5 => false 
6 => Printing after clearing Timeout 

Reference: https://nodejs.org/api/timers.html#timers_class_timeout



Last Updated : 11 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads