JavaScript setTimeout如此不准确的原因是什么? [英] What is the reason JavaScript setTimeout is so inaccurate?

查看:1118
本文介绍了JavaScript setTimeout如此不准确的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里得到了这个代码:

  var date = new Date(); 
setTimeout(function(e){
var currentDate = new Date();
if(currentDate - date> = 1000){
console.log(currentDate,date) ;
console.log(currentDate-date);
}
else {
console.log(不到一秒!);
console。 log(currentDate-date);
}
},1000);

在我的电脑中,它总是正确执行,控制台输出中有1000。有兴趣在其他计算机上,相同的代码,超时回调开始不到一秒钟, currentDate - date 的差异在980和998之间。



我知道存在解决这种不准确性的库(例如,:


除了clamp之外,当页面(或操作系统/浏览器本身)忙于执行其他任务时,超时也可能会触发。


换句话说,通常实现 setTimeout 的方式,它只是意味着在给定的延迟后执行,一旦浏览器的线程可以自由执行它。



但是,不同的浏览器可能以不同的方式实现它。以下是我做过的一些测试:

  var date = new Date(); 
setTimeout(function(e){
var currentDate = new Date();
console.log(currentDate-date);
},1000);

//浏览器测试1测试2测试3测试4
// Chrome 998 1014 998 998
// Firefox 1000 1001 1047 1000
// IE 11 1006 1013 1007 1005

也许<来自Chrome的1000次可归因于 Date 类型的不准确,或者可能是Chrome使用不同的策略来决定何时执行代码—也许它正在尝试即使超时延迟尚未完成,也要将其安装到最近的时隙中。



简而言之,如果您期望可靠,一致,毫秒级的时序,则不应使用 setTimeout


I got this code over here:

var date = new Date();
setTimeout(function(e) {
    var currentDate = new Date();
    if(currentDate - date >= 1000) {
         console.log(currentDate, date);
         console.log(currentDate-date);
    }
    else {
       console.log("It was less than a second!");
       console.log(currentDate-date);
    }
}, 1000);

In my computer, it always executes correctly, with 1000 in the console output. Interestedly in other computer, the same code, the timeout callback starts in less than a second and the difference of currentDate - date is between 980 and 998.

I know the existence of libraries that solve this inaccuracy (for example, Tock).

Basically, my question is: What are the reasons because setTimeout does not fire in the given delay? Could it be the computer that is too slow and the browser automatically tries to adapt to the slowness and fires the event before?

PS: Here is a screenshot of the code and the results executed in the Chrome JavaScript console:

解决方案

It's not supposed to be particularly accurate. There are a number of factors limiting how soon the browser can execute the code; quoting from MDN:

In addition to "clamping", the timeout can also fire later when the page (or the OS/browser itself) is busy with other tasks.

In other words, the way that setTimeout is usually implemented, it is just meant to execute after a given delay, and once the browser's thread is free to execute it.

However, different browsers may implement it in different ways. Here are some tests I did:

var date = new Date();
setTimeout(function(e) {
    var currentDate = new Date();
    console.log(currentDate-date);
}, 1000);

// Browser Test1 Test2 Test3 Test4
// Chrome    998  1014   998   998
// Firefox  1000  1001  1047  1000
// IE 11    1006  1013  1007  1005

Perhaps the < 1000 times from Chrome could be attributed to inaccuracy in the Date type, or perhaps it could be that Chrome uses a different strategy for deciding when to execute the code—maybe it's trying to fit it into the a nearest time slot, even if the timeout delay hasn't completed yet.

In short, you shouldn't use setTimeout if you expect reliable, consistent, millisecond-scale timing.

这篇关于JavaScript setTimeout如此不准确的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆