0

I am trying to pause a video object at a moment. I am baffled at the complex comparison I need to do in order to accomplish this. Maybe it is necessary, but it seems like it should be simpler.

The following does not work:

video.addEventListener("timeupdate", function() {
    if (this.currentTime == video.endTime) {
        this.pause();
    }
}, false);

The following does work:

video.addEventListener("timeupdate", function() {
    if (this.currentTime >= video.endTime && this.currentTime < (video.endTime + 1)) {
        this.pause();
    }
}, false);

How can that be? According to HTML Audio/Video DOM currentTime Property, this value is in seconds. I can only imagine that these seconds are not necessarily whole numbers. If that were the case than I can see a possibility for my listener not to check at the instant the number is whole. This just seems like a silly bit of cruft.

How can I compare for when the two times are equal?

theherk
  • 6,954
  • 3
  • 27
  • 52
  • Far better to use [MDN::HTMLMediaElement](https://developer.mozilla.org/en/docs/Web/API/HTMLMediaElement) than W3Schools. It seems that the properties you seek are *currentTime* and *duration*, there is no *endTime* (though you could add it). Note that there is no specification for these properties, they aren't defined in either the [W3C HTML5](http://www.w3.org/html/wg/drafts/html/CR/embedded-content-0.html#the-video-element) or [WHATWG](http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#the-video-element) specifications. – RobG Mar 17 '14 at 04:38
  • Thank you. That resource is helpful. I have added that variable. The variable is working. However, I don't see why it is failing to match in the first scenario. – theherk Mar 17 '14 at 04:44
  • What do *this* and *video* refer to in `this.currentTime == video.duration`? If the values are decimal seconds, perhaps you need to convert to whole seconds or use a comparison like `currentTime >= duration` rather than `==`. – RobG Mar 17 '14 at 04:48
  • Yes, I think converting to whole seconds might do the trick. I'll look into that. `this` is the current video element and `video` is what the `endTime` variable is attached to. `endTime` is in whole seconds. Is it possible that the currentTime is not an exact whole second at the time of evaluation? It other words, can that return a value other than a whole number and stand the chance of being missed by the listener? – theherk Mar 17 '14 at 04:51

1 Answers1

0

So I can't comment because of my reputation yet but I can seem to get the correct end with

this.seekable.end(0)

Also, I think ">" or "<" work because the "timeupdate" happpens at 250ms or so(sorry I lost the link which told me this)
These will help:
- Control start position and duration of play in HTML5 video
- https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_HTML5_audio_and_video

Community
  • 1
  • 1