在 javascript 中进行投票 [英] poll in javascript

查看:57
本文介绍了在 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:

  1. 您可以轻松地更改迭代之间的延迟.例如在上面,我在 第一次 时间尽快触发检查,然后在随后的每个 100 毫秒之后.你可以用时间做比这更复杂的事情,灵活性就在那里.

  1. 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 在浏览器中是从上次调用的 start 还是从它的 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 毫秒),至少在某些浏览器上是这样.

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.

当然,这取决于您,使用 setIntervalclearInterval 调用与使用 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屋!

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