Skip to content
Related Articles

Related Articles

Node.js Timeout Timer Class
  • Last Updated : 13 Aug, 2020

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, exports both timeout.ref() and timeout.unref() functions.

The four Timeout Class objects are defined below:

  1. 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’.

  2. 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 exit so long. Anyway, calling this Method multiple times does not have any effect.

    Syntax:

    timeout.ref()

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

  3. 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 already called its callback and then it reactivates the timer.

    Syntax:

    timeout.refresh()

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

  4. 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: Filename: index.js




// Node.js program to demonstrate the
// Timeout Class methods
  
// Setting Timeout by setTimeout Method
var 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 referance 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 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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :