Node.js – Timeout-hasRef() & Timeout-refresh() methods

The Timeout object is internally created and is returned from the setTimeout() and setInterval() method. You can use this object and pass it to either clearTimeout() or clearInterval() methods in order to cancel the scheduled actions

Following are the timeout class ref objects that can be used to control the default behaviour

1. timeout.hasRef()

This method keeps the node event loop active as long as its value is True.



2. timeout.refresh()

This method refreshes the timer’s start time to the current time and reschedules the timer to its callback where the previously specified duration will be adjusted to the current time. This method helps in refreshing the timer without a new JS object.




Create a file with the name "timeout.js" and copy the following code snippet. After creating the file, use the command "node timeout.js" to run this code.

// Timeout class Demo Example

// Setting the Timeout using setTimeout() Method
var Timeout = setTimeout(function fun() {
   console.log("1. Setting Timeout for 100ms", 100);

// Checking if the timeout.hasRef() object
console.log("2. ", Timeout.hasRef());

// Refreshing the timer
console.log("3. ", Timeout.refresh());

// Clears setInterval Timeout

console.log("4. Timeout is cleared !");


2. true
3. Timeout {
   _called: false,
   _idleTimeout: 1,
   _idlePrev: [TimersList],
   _idleNext: [TimersList],
   _idleStart: 382,
   _onTimeout: [Function: alfa],
   _timerArgs: undefined,
   _repeat: null,
   _destroyed: false,
   [Symbol(unrefed)]: false,
   [Symbol(asyncId)]: 5,
   [Symbol(triggerId)]: 1 }
4. Timeout is cleared !

Updated on: 29-Oct-2021


Kickstart Your Career

Get certified by completing the course

Get Started