setInterval延迟不准确 [英] setInterval delays not accurate

查看:171
本文介绍了setInterval延迟不准确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用setInterval创建一个倒计时,虽然它运行得比它应该慢。根据 MDN ,延迟参数以毫秒为单位,但它不准确。

I'm currently creating a countdown using setInterval though at the moment it runs slower than it should. According to the MDN, the delay parameter is in milliseconds however it isn't accurate.

我将倒计时与手机上的倒计时进行了比较,手机的运行速度提高了近5倍。

I compared my countdown to the one on my phone and the phone runs nearly 5 times faster.

    var count = setInterval( function() {
            if (iMil == 0) {
                if (iS == 0) {
                    if (iMin == 0) {
                        if (iH == 0) {
                            // DONE
                        } else {
                            iH--;
                            iMin = 59;
                            iS = 59;
                            iMil = 999;
                        }
                    } else {
                        iMin--;
                        iS = 59;
                        iMil == 999;
                    }
                } else {
                    iS--;
                    iMil = 999;
                }
            } else {
                iMil--;
            }
            hours.text(iH);
            minutes.text(iMin);
            seconds.text(iS);
            milliseconds.text(iMil);
        }, 1 );

这是我脚本的主要部分。变量小时分钟毫秒是jQuery对象元素。

This is the main part of my script. The variables hours, minutes, seconds and milliseconds are jQuery object elements.

我得到的是,它运行速度是否比它更慢假设呢?

What I'm getting at is, is there a reason that it runs slower than it is supposed too?

推荐答案

setInterval()无法保证完美运行按时在javascript中。这部分是因为JS是单线程的,部分原因是其他原因。如果你想用 setInterval()显示时间,那么在每个计时器勾选时获取当前时间并显示它。 setInterval()将不是您的计时器,而只是一个重复出现的屏幕更新机制。如果你这样做的话,你的时间显示总是准确的。

setInterval() is not guaranteed to run perfectly on time in javascript. That's partly because JS is single threaded and partly for other reasons. If you want to display a time with setInterval() then get the current time on each timer tick and display that. The setInterval() won't be your timer, but just a recurring screen update mechanism. Your time display will always be accurate if you do it that way.

此外,没有浏览器会保证以1ms的间隔调用你的间隔。事实上,许多浏览器永远不会比每隔5毫秒调用 setInterval ,有些甚至更长。另外,如果浏览器中发生任何其他事件并且其他代码响应这些事件,则 setInterval()调用可能会延迟更长时间。 HTML5规范建议将4ms作为 setTimeout()的最短间隔,将10ms作为 setInterval()的最短间隔,但如果需要,允许实现者使用更长的最短时间。

In addition, no browser will guarantee a call to your interval at 1ms intervals. In fact, many browsers will never call setInterval more often than every 5ms and some even longer than that. Plus, if there are any other events happening in the browser with other code responding to those events, the setInterval() call might be delayed even longer. The HTML5 spec proposes 4ms as the shortest interval for setTimeout() and 10ms as the shortest interval for setInterval(), but allows the implementor to use longer minimum times if desired.

事实上,如果你看一下此计划草案规范,算法的第5步说:

In fact, if you look at this draft spec for timers, step 5 of the algorithm says:


如果超时小于10,则将超时增加到10。

If timeout is less than 10, then increase timeout to 10.

并且,步骤8说这个:


(可选)等待另一个用户代理定义的时间长度。

Optionally, wait a further user-agent defined length of time.

并且,它包含此注释:


这是为了允许用户代理根据需要填充超时
优化设备的功耗。例如,某些处理器
具有低功耗模式,其中定时器的粒度减小;在
这样的平台上,用户代理可以减慢定时器的速度以适应这个时间表
,而不是要求处理器使用更准确的模式与
相关的更高功率使用。

This is intended to allow user agents to pad timeouts as needed to optimise the power usage of the device. For example, some processors have a low-power mode where the granularity of timers is reduced; on such platforms, user agents can slow timers down to fit this schedule instead of requiring the processor to use the more accurate mode with its associated higher power usage.

这篇关于setInterval延迟不准确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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