Node.js – hasRef() 和 refresh() 定時器方法
定時器物件透過內部建立,並由 setTimeout() 和 setInterval() 方法返回。您可以使用此物件,並將其傳遞給 clearTimeout() 或 clearInterval() 方法,以便取消已排程的操作
以下是用於控制預設行為的 超時類 ref 物件
1. timeout.hasRef()
只要它的值為 True,此方法就能保持 Node 事件迴圈處於活動狀態。
語法
timeout.hasRef()
2. timeout.refresh()
此方法將計時器的開始時間重新整理為當前時間,並將計時器重新排程到回撥函式中,此前指定的時間間隔將調整為當前時間。此方法有助於在沒有新 JS 物件的情況下重新整理計時器。
語法
timeout.refresh()
示例
建立一個名為 "timeout.js" 的檔案並複製程式碼段。建立檔案之後,使用命令 "node timeout.js" 來執行此程式碼。
// 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 !");輸出
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 !
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP