Web Workers:SetInterval和SetTimeout可靠性 [英] Web Workers: SetInterval and SetTimeout reliability

查看:179
本文介绍了Web Workers:SetInterval和SetTimeout可靠性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在浏览器环境中, setTimeout setInterval 在准确性方面不可靠 - 时间敏感度是一个问题。

In a browser environment, setTimeout and setInterval aren't reliable for accuracy - where time sensitivity is an issue.

最近,我们获得了 requestAnimationFrame ,这使我们能够做得更好(就 performance.now()及其时间戳)。

Recently, we've been given requestAnimationFrame, which allows us to do a little better (in terms of performance.now() and its timestamps).

长话短说 - 我决定用Javascript构建一个节拍器虽然它有效,但它在某个节奏之上是相当不准确的。虽然补偿后期帧可以使节奏不随时间变化,但是个别节拍略微偏离,这对节拍器不起作用。 (这对于动画来说不是问题,因为它本质上不需要是离散的。)

Long story short - I decided to build a metronome in Javascript, and while it works, it's fairly inaccurate above a certain tempo. While compensating for late frames allows the tempo not to desync over time, the individual beats are slightly off, which doesn't work for a metronome. (This is not a problem for animation, as it by nature doesn't need to be discrete.)

现在,我可以选择尝试执行前瞻基于我指定的阈值,尝试在可用帧之间放置点击(在动画循环中使用 setTimeout )。但我想,由于事件循环,我会遇到类似的问题,因为 setTimeout 在浏览器中不可靠,除非HTML5 Audio API允许你延迟播放几毫秒。

Right now, I have the option of attempting to perform a lookahead based on a threshold that I specify, to attempt to place the click between available frames (using setTimeout in the animation loop). I imagine, though, that I'll run into similar problems as setTimeout isn't reliable in the browser due to the event loop, unless the HTML5 Audio API will allow you to delay playback by a number of milliseconds.

我的问题: setTimeout setInterval 在Web工作者和浏览器环境中更准确可靠吗?

My question: Are setTimeout and setInterval more accurate and reliable in a web worker vs the browser environment?

推荐答案

我们可以说'是'对于你的问题,Web worker对settimeout和setinterval函数是可靠的,因为web worker根据ui在后台运行,因此它们为你提供非阻塞的ui事件(它们可能会影响节拍器速度),同时你正在处理持续的节拍器时序。

We can say 'yes' for your question, Web workers are reliable for settimeout and setinterval functions,because web workers runs on background according to ui, so they provide you non-blocking ui events(they might affect metronome tempo), while you are processing the continuing metronome timing.

顺便说一下,在此处

这篇关于Web Workers:SetInterval和SetTimeout可靠性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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