17

I have a HTML5 video element in my page. The video I want to play is having a duration of 10 minutes.

I have to play the part of the video from minute 1 to minute 5.
I can start it from a particular time by setting its currentTime property.
But how can I stop the video at a particular time jQuery or JavaScript?

insertusernamehere
  • 21,742
  • 7
  • 80
  • 113
Arvin
  • 796
  • 3
  • 10
  • 32

4 Answers4

48

TL;DR: Simply listen on "timeupdate":

video.addEventListener("timeupdate", function(){
    if(this.currentTime >= 5 * 60) {
        this.pause();
    }
});

The usual way to wait for something in JavaScript is to wait for an event or a timeout. A timeout is out of question in this case, the user might pause the video on his own. In this case the stop wouldn't be on your specific time, but earlier.

Checking the time regularly is also too costly: you either check too often (and therefore waste precious processing power) or not often enough and therefore you won't stop at the correct time.

However currentTime is a checkable property, and to our luck, there's the timeupdate event for media elements, which is described as follows:

The current playback position changed as part of normal playback or in an especially interesting way, for example discontinuously.

This concludes that you can simply listen on timeupdate and then check whether you've passed the mark:

// listen on the event
video.addEventListener("timeupdate", function(){
    // check whether we have passed 5 minutes,
    // current time is given in seconds
    if(this.currentTime >= 5 * 60) {
        // pause the playback
        this.pause();
    }
});

Keep in mind that this will pause whenever the user tries to skip past 5 minutes. If you want to allow skips and only initially pause the video beyond the 5 minute mark, either remove the event listener or introduce some kind of flag:

var pausing_function = function(){
    if(this.currentTime >= 5 * 60) {
        this.pause();

        // remove the event listener after you paused the playback
        this.removeEventListener("timeupdate",pausing_function);
    }
};

video.addEventListener("timeupdate", pausing_function);
Zeta
  • 95,453
  • 12
  • 173
  • 214
  • currentTime is in seconds, not miliseconds – apostl3pol Sep 01 '15 at 04:43
  • @apostl3pol: Thanks. Note that you can suggest edits if you find an old answer. That way, you can improve (or fix) old answers, even if the original author isn't active anymore. – Zeta Sep 01 '15 at 06:14
  • is it neccesary to do var pausing_function = function() {}? couldnt it be function pausing_function(){} – DGoiko Jan 26 '19 at 22:06
  • @DGoiko https://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname – Zeta Jan 27 '19 at 08:15
3

The timeupdate event is what you are looking for, but it only fires at about 2 fps which is too slow to stop at precise times.

For those cases I used requestAnimationFrame which fires at 60 fps and decreased the endTime a little which fixes small "lag hops":

const onTimeUpdate = () => {
    if (video.currentTime >= (endTime - 0.05)) {
      video.pause()
    } else {
      window.requestAnimationFrame(onTimeUpdate)
    }
}
window.requestAnimationFrame(onTimeUpdate)
Fabian von Ellerts
  • 3,261
  • 28
  • 31
0

So as below

<video id="myVid">
<source></source> <!--Whatever source here -->
</video>

using Above HTML attach an Event

var vid = document.getElementById("myVid");
vid.addEventListener("timeupdate", function(){
// Check you time here and
if(t >= 300000) //Where t = CurrentTime
{
vid.stop();// Stop the Video
}
});

This is the right way of doing it.

Akshay Khandelwal
  • 1,542
  • 11
  • 19
-1

Not sure of an inbuilt way, but one way would be to use the setInterval Function and check the currentTime for the video and then stop playback

var myVid=document.getElementById("video1");
var timer= setInterval(function(){myTimer()},1000);
function myTimer()
  {
    if(myVid.currentTime == 5* 60)
    {
       myVid.pause();
       window.clearInterval(timer);
    }
  }
Prat
  • 485
  • 7
  • 19