简体   繁体   English

setTimeout的最小毫秒值是多少?

[英]What is minimum millisecond value of setTimeout?

I would like to put 我想放

var minValue = 0;
if ( typeof callback == 'function' ) {
    setTimeout( callback, minValue );
}

this code when I implement callback function with JavaScript. 当我使用JavaScript实现回调函数时,此代码。

But I've found that modern browsers and some old browsers 但是我发现现代的浏览器和一些旧的浏览器

have different minimum timeout value. 具有不同的最小超时值。

I know that Zero cannot be minimum value. 我知道零不能是最小值。

What would be minimum value of setTimeout for setTimeout的最小值是多少

modern browsers and some old browsers for compatibility issues? 现代浏览器和某些旧版浏览器是否存在兼容性问题?

I think that 10 will be the most reliable minimum in all browser, since I've seen a lot of codes using it. 我认为10是所有浏览器中最可靠的最小值,因为我已经看到很多使用它的代码。

However, 4ms is the minimum for HTML5 但是, HTML5最少需要4毫秒

In fact, 4ms is specified by the HTML5 spec and is consistent across browsers released in 2010 and onward. 实际上,HTML5规范指定了4毫秒,并且在2010年及以后发布的浏览器中保持一致。 Prior to (Firefox 5.0 / Thunderbird 5.0 / SeaMonkey 2.2) , the minimum timeout value for nested timeouts was 10 ms. 在(Firefox 5.0 / Thunderbird 5.0 / SeaMonkey 2.2)之前,嵌套超时的最小超时值为10 ms。

The minimum is 4ms (as of HTML5) in modern browser, prior to that, it was 10ms. 在现代浏览器中,最小值为4毫秒(从HTML5开始),在此之前为10毫秒。 Note that these times are never 100% accurate. 请注意,这些时间永远不会100%准确。

setTimeout is most probably calling the sleep or Sleep system call . setTimeout最有可能调用sleepSleep系统调用

The actual mechanics, including the minimum amount of milliseconds, of setTimeout are proprietary and/or system-dependent, since they are not in the official ECMA specs. setTimeout的实际机制(包括最短毫秒)是专有的和/或系统相关的,因为它们不在ECMA官方规范中。 It depends on your Javascript run-time, as well as the system you are running it on. 它取决于Javascript运行时以及运行它的系统。 Given, your Javascript run-time does not add a whole lot of overhead, the minimum amount of milliseconds is determined by the timeslice resolution of your operating system and hardware. 给定的是,您的Javascript运行时不会增加很多开销,最小毫秒数由操作系统和硬件的时间片分辨率确定。 The smallest "sleepable" amount of time is usually the time it takes for the process to be allocated another timeslice by your system's scheduling algorithm . 最小的“可睡眠”时间通常是系统的调度算法将进程分配给另一个时间片所花费的时间

On Windows (post XP) for example, the documentation for the sleep system call reveals: 例如,在Windows(XP后)上, 睡眠系统调用的文档显示:

A value of zero causes the thread to relinquish the remainder of its time slice to any other thread that is ready to run. 值为零会导致线程将其时间片的其余部分放弃给准备运行的任何其他线程。 If there are no other threads ready to run, the function returns immediately, and the thread continues execution. 如果没有其他线程可以运行,该函数将立即返回,并继续执行该线程。

That means, under some extremely rare conditions, where there is no other process currently waiting on the hardware thread that your Javascript run-time process is running on, it might continue immediately after the caller finished executing, depending on how your Javascript run-time is implemented. 这意味着,在某些极为罕见的情况下,当前没有其他进程正在等待运行Javascript运行时进程硬件线程 ,它可能在调用者执行完后立即继续运行,具体取决于Javascript运行时的方式被实施。 You will probably not observe such condition very often though :) 您可能不会经常观察到这种情况:)

This article tests Firefox, Safari, and Opera and plots performance graphs: 本文测试Firefox,Safari和Opera,并绘制性能图:

http://ejohn.org/blog/analyzing-timer-performance/ http://ejohn.org/blog/analyzing-timer-performance/

Firefox 2, Opera, and Safari all have a bottom window of 10ms for delays Firefox 2,Opera和Safari的底部都有10毫秒的延迟窗口

For older browsers, you can do a test like the one in that article. 对于较旧的浏览器,您可以像该文章中那样进行测试。 I just ran a test that I had from a while ago of setInterval using a 10ms interval in IE6, and I got an average of 55ms . 我刚刚使用IE6中的10ms间隔运行了一段时间前的setInterval进行的测试,平均得到了55ms setTimeout seems to be lower at 35ms . setTimeout似乎更低,为35ms

I ran the test in Chromium and got ~11ms average for a 10ms timeout. 我在Chromium中进行了测试,并在10ms的超时时间内获得了平均11ms的时间。 I tried it with 4ms and 1ms intervals and got ~4.5ms for both. 我以4ms和1ms的间隔尝试了一下,两者都达到了〜4.5ms Also, keep in mind that the numbers could vary among operating systems. 另外,请记住,数字在操作系统之间可能会有所不同。

If you're interested, here's the test code: 如果您有兴趣,这里是测试代码:

<script>
// number of times to call setTimeout before calculating average
var ITERATIONS = 200;

window.onload = function()
{
    testTimeout(10, +new Date, 0, 0);
}

// calls setTimeout repeatedly at a specified interval, tracking the amount
// of time that passes between successive calls
function testTimeout(interval, last, sum, ii)
{
    var time = +new Date;
    var difference = time - last;
    sum += difference;
    if (ii % ITERATIONS == 1)
    {
        document.body.innerHTML = sum / ITERATIONS;
        sum = 0;
    }
    window.setTimeout(
        function() {
            testTimeout(interval, time, sum, ii + 1)
        }, interval);
}
</script>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM