0

I want to play a HTML5 video in segments of x seconds. For example, start at t=0 and pause at t=x, then start at t=x and pause at t=2x. The problem is that the updates I receive with ontimeupdate don't match my intervals. There is one update short before the intended stop time and one update shortly after. I could just stop the video whenever currentTime >= x, but the problem here is that this stop point would fall into the new interval. The requirement for this task is to stop at the end of a given interval.

If stopping exactly at a given time is not possible, is there any way to determine the closest possible stop time before that time? That would still be better than stopping too late. I checked the deltas of currentTime (time between each ontimeupdate call), but these are not constant. I read somewhere else that the browser adapts this rate based on some optimization criterions, so that is probably hard to compute.

Background for this question is a tool that I want to develop. The user is shown a video and he is required to answer some questions for each x second interval of this video.

Simon Hessner
  • 1,757
  • 1
  • 22
  • 49

1 Answers1

1

Unfortunately the timeupdate event doesn't provide a lot of granularity. You can read this answer for more information.

One alternative is to set up an interval manually with setInterval, and on each interval check the time passed with Date.now() since the last time that the timeupdate was updated (also using Date.now() and saving that value somewhere).

That would allow knowing the milliseconds from then, although it would need to handle cases like pausing the video and clearing the interval when necessary to avoid memory leaks with clearTimeout.

Ignacio
  • 688
  • 5
  • 17
  • Thanks. I'll probably go with the interval solution. I also found another way to get more accurate times (using `requestAnimationFrame`), but probably the interval is the best way to get as closely as possible to the desired time because the animation frame way only gives me a 60hz rate... – Simon Hessner May 09 '20 at 19:34