Web Workers: SetInterval and SetTimeout reliability

吃可爱长大的小学妹 提交于 2019-12-10 02:20:33

问题


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

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

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.)

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.

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


回答1:


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.

By the way there is a good doc about web workers in here.



来源:https://stackoverflow.com/questions/27162254/web-workers-setinterval-and-settimeout-reliability

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!