简体   繁体   English

为什么setTimeout计时器触发两次?

[英]Why does the setTimeout timer fire twice?

I have a Timeout timer that fires on the minute based on the system clock. 我有一个超时计时器,它根据系统时钟在分钟内触发。 The thing I don't understand is why it fires twice every minute? 我不明白的是为什么它每分钟触发两次? If you check the console log it fires twice http://jsbin.com/vises/3/ 如果您检查控制台日志,则会触发两次http://jsbin.com/vises/3/

function runClock() {

var now = new Date();
var timeToNextTick = (60 - now.getSeconds()) * 1000 - now.getMilliseconds();

console.log((60 - now.getSeconds()* 1000)- now.getMilliseconds());
setTimeout(runClock, timeToNextTick);
}  

// start it
runClock();

this is the console log: 这是控制台日志:

在此处输入图片说明

It is most likely an issue with javascript timers not being very accurate, you could try something like this instead: javascript计时器不是很准确,这很可能是一个问题,您可以尝试使用以下方法:
http://fiddle.jshell.net/7ffv00jy/ http://fiddle.jshell.net/7ffv00jy/

where the big change is that you test to see if the timeUntilNextTick is very small, then you should wait a full more minute (since you probably just fired one of these events anyways..) 最大的变化是您要测试timeUntilNextTick是否很小,然后您应该等待一整分钟(因为无论如何您可能都触发了其中一个事件。)
You could also have a timeout to find the first minute, then have that "starter" function set up an interval instead. 您也可能会有超时时间来查找第一分钟,然后让“启动器”功能设置一个间隔。

Here is the changed code: 这是更改后的代码:

function runClock() {
    var timeToNextTick = msUntilNextMinuteTick();

    if(timeToNextTick < 100)
        timeToNextTick += 60*1000; // Add a full minute to this..
    console.log(new Date().toString() + ": " + timeToNextTick);
    setTimeout(runClock, timeToNextTick);
}

// Separate function, makes it easier to debug
function msUntilNextMinuteTick(){
    var now = new Date();
    return (60 - now.getSeconds()) * 1000 - now.getMilliseconds();    
}

// start it
runClock();

so far it has given me this: 到目前为止,它已经给了我:

Wed Oct 15 2014 13:36:22 GMT+0200 (W. Europe Summer Time): 37035   
Wed Oct 15 2014 13:37:00 GMT+0200 (W. Europe Summer Time): 59790  
Wed Oct 15 2014 13:38:00 GMT+0200 (W. Europe Summer Time): 59789  
Wed Oct 15 2014 13:39:00 GMT+0200 (W. Europe Summer Time): 59789  
Wed Oct 15 2014 13:40:00 GMT+0200 (W. Europe Summer Time): 59789  

(The important part here is the date time at the beginning which, with the exception of the "starter" all ends with :00 seconds (if you print the ms part they would probably not all end with .00000)) (这里重要的部分是开始的日期时间,除了“启动器”以外,所有时间都以:00秒结尾(如果打印ms部分,则它们可能不会全部以.00000结尾)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么setTimeout会在Chrome中触发两次,而不是IE或Firefox? - Why does setTimeout fire twice in Chrome, but not IE or Firefox? 在模块化功能中重置计时器似乎会导致计时器无法启动。 为什么setTimeout不起作用? - Resetting a timer within a modular function seems to cause timer not to fire. Why doesn't the setTimeout work? 为什么我的JavaScript onresize会触发两次? - Why does my JavaScript onresize fire twice? 为什么 jQuery resize 事件会触发两次? - Why does the jQuery resize event fire twice? 在我“重置”它之后,setTimeout 是否有触发两次的风险? - Is there a risk that setTimeout will fire twice after I “reset” it? 为什么 Map 在严格模式下使用 setTimeout 和 React 更新两次 - Why does Map updated twice with setTimeout and React in Strict Mode 为什么此Ember.computed属性的观察者会触发两次? - Why does this Ember.computed property's observer fire twice? 为什么 MutationObserver 为 childList 触发两次但从未为 characterData 触发? - Why does MutationObserver fire twice for childList but never for characterData? Ckeditor-为什么我的代码两次触发? 以及如何解决? - Ckeditor - why does my code fire twice? And how to fix it? 为什么 iOS Safari 两次触发窗口焦点事件? - Why does iOS Safari fire window focus event twice?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM