用JavaScript轮询 [英] poll in javascript
问题描述
我正在使用[youtube api] [1]了解视频何时已完全缓冲player.getVideoLoadedFraction()
I'm using [youtube api ][1] to get to know when a video is fully buffered player.getVideoLoadedFraction()
当小数为1时,视频已完全缓冲,但是我必须轮询此函数以检查它是否为1,然后获取时间,例如:
when the fraction is 1, the video is fully buffered, but I have to poll this function to check whether it is 1 and then get the time, like:
setInterval(display_fraction,1);
因为视频可能长达数十分钟.
since a video could be tens of minutes.
此轮询会给浏览器/客户端带来沉重的负担,从而影响视频流吗?还有其他更好的轮询方法或方法来检测youtube何时完成缓冲吗?
Will this polling creates a heavy load on the browser/client and thus affect the video streaming? are there any other better polling methods or ways to detect when youtube finishes buffering?
顺便说一句,youtube api的链接是: https://developers.google.com/youtube/flash_api_reference#Playback_controls
BTW, the link for youtube api is: https://developers.google.com/youtube/flash_api_reference#Playback_controls
推荐答案
人类开始感知到20秒到10秒之间的时间间隔,因此尝试以1ms的值进行轮询既无必要,也不可取(任何现代方法浏览器会将其舍入为5毫秒或10毫秒).像50或100这样的值会更合适.
Humans start perceiving time intervals somewhere between a 20th and a 10th of a second, so trying to poll with a value of 1ms is neither necessary nor desireable (any modern browser will round that up to 5ms or 10ms anyway). Values like 50 or 100 would be more appropriate.
我也强烈建议使用一系列链接的setTimeout
调用而不是setInterval
调用,例如:
I would also strongly recommend using a chained series of setTimeout
calls rather than a setInterval
call, something like this:
function onVideoReady(callback) {
// Do first check as soon as the JavaScript engine is available to do it
setTimeout(checkVideoReady, 0);
// Our check function
function checkVideoReady() {
if (videoIsReady) {
// The video is ready, notify calling code
callback();
}
else {
// Not ready yet, wait a 10th of a second
setTimeout(checkVideoReady, 100);
}
}
}
...然后您可以像这样使用
...which you then use like this:
onVideoReady(function() {
// The video is ready, do something
});
我主张使用链式系列setTimeout
而不是setInterval
的原因是:
The reasons I advocate a chained series of setTimeout
instead of setInterval
are:
-
您可以轻松地在迭代之间更改延迟.例如,在上面的示例中,我会在第一时间尽快触发检查,然后在随后的每次100ms之后触发.您可以用定时来做更复杂的事情,灵活性就在那里.
You can change the delay easily from iteration to iteration. For instance in the above, I fire the check as soon as possible the first time, then then after 100ms each subsequent time. You can do more complex things with timing than that, the flexibility is there.
无意中结束多次运行会变得非常困难,因为代码必须显式触发下一个循环.
It's much, much harder to inadvertently end up with more than one running, since code has to explicitly trigger the next loop.
setInterval
对于从最后一次调用的开始还是从最后一次调用的结束来测量接口而有所不同.如果您使用上述模式,则始终确保它来自上次检查的 end .
setInterval
varies amongst browsers about whether it measure the interface from the start of the last call or the end of it. If you use a pattern like the above, you're always sure it's from the end of the last check.
如果在下一个间隔发生时您的代码仍在运行,则会跳过该间隔.至少在某些浏览器上,这可能会导致间隙(例如,如果您每100毫秒执行一次操作,而您的上一个循环需要102毫秒才能完成,那么下一个循环不会尽快开始,而是等待其余的98毫秒)./p>
If your code is still running when the next interval is meant to occur, it just gets skipped. This can cause gaps (e.g., if you're doing something every 100ms and your previous loop takes 102ms to complete, the next one doesn't start as soon as possible, it waits the remaining 98ms), at least on some browsers.
但是,这完全取决于您,当然,使用setInterval
和clearInterval
调用可以像使用一系列setTimeout
调用一样轻松地完成上述操作.
But it's up to you, of course, the above can be done just as easily with setInterval
and clearInterval
calls as with a chain of setTimeout
calls.
这篇关于用JavaScript轮询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!