How often does the timeupdate event fire for an html5 video

前端 未结 3 1162
梦谈多话
梦谈多话 2020-11-30 11:32

Learning html5 stuff. It\'s pretty awesome! Wondering how often the timeupdate event fires.

SIDE NOTE: There are so many interesting possibilities with the js video

3条回答
  •  -上瘾入骨i
    2020-11-30 12:11

    I used a generic throttle function

    _self.throttle = function (fn, threshhold, scope) {
        threshhold || (threshhold = 250);
        var last,
            deferTimer;
        return function () {
            var context = scope || this;
    
            var now = +new Date,
                args = arguments;
            if (last && now < last + threshhold) {
                // hold on to it
                clearTimeout(deferTimer);
                deferTimer = setTimeout(function () {
                    last = now;
                    fn.apply(context, args);
                }, threshhold);
            } else {
                last = now;
                fn.apply(context, args);
            }
        };
    };
    

    and wired it up with

    myPlayer.on('timeupdate', window.vm.throttle(function () {
            window.vm.setWatched(myPlayer.currentTime());
        }, 3000));
    

    hope this helps someone.

    code cribbed from http://remysharp.com/2010/07/21/throttling-function-calls/

提交回复
热议问题