Node.js Timeout Timer Class - GeeksforGeeks

18 Jun, 2021
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 
Javascript
// Node.js program to demonstrate the// Timeout Class methods // Setting Timeout by setTimeout Methodvar Timeout = setTimeout(function alfa() {    console.log("0.> Setting Timeout", 12);}); // Printing Timeout.hasRef methodconsole.log("1 =>", Timeout.hasRef());// returns true // Printing Timeout.ref before unrefconsole.log("2 =>", Timeout.ref());Timeout.unref()Timeout.ref()// Returns timer reference // Printing Timeout.refresh before unrefconsole.log("3 =>", Timeout.refresh());// Returns timer reference // Printing Timeout.unref methodconsole.log("4 =>", Timeout.unref());// Returns Timeout reference and sets// hasRef to false // Printing Timeout.hasRef before unrefconsole.log("5 =>", Timeout.hasRef());// Returns false // Clears setInterval TimeoutclearTimeout(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
saurabh1990aror
Node.js-Basics
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to update Node.js and NPM to next version ?
Node.js fs.readFileSync() Method
Node.js fs.writeFile() Method
How to update NPM ?
Difference between promise and async await in Node.js
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?