javascript - 如何查看间隔中剩余的时间
问题描述
我正在查看超时对象的时间间隔,但我看不到任何方法实际上可以确定该时间间隔再次运行多长时间。
我在节点文档中看不到任何指向其工作原理的内容,并且大多数解释都只是谈论“倒计时的计时器线程”,对此我找不到更多信息。
任何线索我如何才能看到或计算它再次运行多长时间?在最新节点上运行以防万一
对于上下文,我正在使用的计时器 (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 }
解决方案
我认为是基于 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);
推荐阅读
- react-native - GoogleAPIs 与 React Native / Expo 的集成
- laravel - 多输入搜索表单
- html - 如何在 ::-webkit-scrollbar-button 中添加伪元素
- python - Discord.py:如何从用户输入消息中获取命令的确切字词?
- video - 怎么给视频加水印。即 png 和 text
- file - Visual Studio Code 打开多个实例
- flutter - 如何在 Flutter 中集成 PayPal 订阅?
- javascript - 三角形最小路径总和自上而下而不是自下而上
- python - 400 使用 TLS 包装器重放捕获的 HTTP/2 请求数据包时来自某些网站的错误请求
- amazon-web-services - 在 IAM 中列出特定任务所需的所有权限的最简单方法是什么?