Node.js – Timeout-hasRef() 和 Timeout-refresh() 方法
该超时对象在内部创建并从返回setTimeout()和setInterval()方法。您可以使用此对象并将其传递给clearTimeout()或clearInterval()方法以取消计划的操作
以下是可用于控制默认行为的超时类引用对象
1.timeout.hasRef()
只要其值为True,此方法就会使节点事件循环保持活动状态。
语法
timeout.hasRef()
2.timeout.refresh()
此方法将计时器的开始时间刷新为当前时间,并将计时器重新安排到其回调,其中先前指定的持续时间将调整为当前时间。此方法有助于在没有新JS对象的情况下刷新计时器。
语法
timeout.refresh()
示例
创建一个名为“timeout.js”的文件并复制以下代码片段。创建文件后,使用命令“nodetimeout.js”运行此代码。
//超时类演示示例 //使用setTimeout()方法设置超时 var Timeout = setTimeout(function fun() { console.log("1. Setting Timeout for 100ms", 100); }); //检查timeout.hasRef()对象 console.log("2. ", Timeout.hasRef()); //刷新计时器 console.log("3. ", Timeout.refresh()); //清除setInterval超时 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 !