- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Syntax
timeout.hasRef()
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.
Syntax
timeout.refresh()
Example
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 clearTimeout(Timeout); console.log("4. Timeout is cleared !");
Output
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 !