首页 > 解决方案 > 如何查看间隔中剩余的时间

问题描述

我正在查看超时对象的时间间隔,但我看不到任何方法实际上可以确定该时间间隔再次运行多长时间。

我在节点文档中看不到任何指向其工作原理的内容,并且大多数解释都只是谈论“倒计时的计时器线程”,对此我找不到更多信息。

任何线索我如何才能看到或计算它再次运行多长时间?在最新节点上运行以防万一

对于上下文,我正在使用的计时器 (setInterval) 是一个小时长

Timeout {
 _idleTimeout: 3600000,
 _idlePrev: [Timeout],
 _idleNext: [Timeout],
 _idleStart: 5010,
 _onTimeout: [Function],
 _timerArgs: undefined,
 _repeat: 3600000,
 _destroyed: false,
 [Symbol(refed)]: true,
 [Symbol(asyncId)]: 1622,
 [Symbol(triggerId)]: 0 }

标签: javascriptnode.jstimer

解决方案


我认为是基于 node.js 源

const Timer = process.binding('timer_wrap').Timer;


const timeLeft = timer => {
    timer.timeLeft = function () {
        if (this._called) return 0;
        return timer._idleStart + timer._idleTimeout - Timer.now()
    };
    return timer;
}


const timer = timeLeft(setTimeout(_ => console.log('hi'), 1000));

setInterval(_ => console.log('Time left: ', timer.timeLeft()), 100);

推荐阅读