18

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 api. For instance, it might be possible to use ctrl + F to search through video. Run a voice recognition as part of video processing, then create a long key value store with timestamps as keys and words as values, and write a function that searches for instances of those words, but returns timestamps and seeks your video. Anyways, that's just one crazy idea youtube should jump on.

Any help with timeupdate would be awesome!

5
  • Hey, this word search is a pretty cool idea! Commented Aug 30, 2013 at 15:31
  • Know anyone that works at Vimeo or YouTube who might implement it? Commented Aug 31, 2013 at 16:31
  • Nope. It will have its own chalenges. It is trivial to index the video if it has subtitles, but if it hasn't, then server has to automate speech to text. Since it own't always be a 5-10 second video, may be around 10 minutes long or so, the process has to convert a substantially long audio to text, which itself is difficult, accuracy aside. Commented Aug 31, 2013 at 18:59
  • Sounds right to me. What do you think, worthy challenges, or not worth it? Commented Sep 1, 2013 at 21:21
  • 1
    Worthy, definitely.. :) Commented Sep 2, 2013 at 6:55

3 Answers 3

26

According to this Bugzilla page:

Firefox fires the timeupdate event once per frame. Safari 5 and Chrome 6 fire every 250ms. Opera 10.50 fires every 200ms.

4
  • 5
    Go Firefox btw, that makes so much sense to do it once per frame. Commented Mar 21, 2012 at 22:00
  • 2
    Unless your binding some kind of callback; firing a callback 25 times a second can be quite intensive... particularly when you can't throttle it. Commented May 13, 2013 at 1:40
  • How else might you let a program know to execute a function at a particular moment of a video? Commented Aug 31, 2013 at 16:32
  • see my answer if it helps!!! stackoverflow.com/questions/4460263/…
    – vinesh
    Commented Jul 14, 2015 at 4:42
3

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/

1
  • Does the myPlayer = document.querySelector('audio') ? Since the Uncaught TypeError: Object #<HTMLAudioElement> has no method 'on' Commented Oct 7, 2013 at 23:07
3

If you only need to run a function every so often it'd be a better idea to run it using the play and pause events.

Below is an example of running a process every 3 seconds while the video is playing.

video.addEventListener('play', () => {
  video._updateInterval = setInterval(() => {
    // do what you need
  }, 3000);
}, true);

video.addEventListener('pause', () => clearInterval(video._updateInterval), true);

Not the answer you're looking for? Browse other questions tagged or ask your own question.