Skip to content

Instantly share code, notes, and snippets.

@fhdalikhan
Created October 23, 2020 11:32
Show Gist options
  • Save fhdalikhan/98e528088cd3f8108c5e29d35633bd65 to your computer and use it in GitHub Desktop.
Save fhdalikhan/98e528088cd3f8108c5e29d35633bd65 to your computer and use it in GitHub Desktop.
JavaScript play video for n seconds and then pause.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<video id="mejs_837308676760252_html5" controls="true" height="200" width="300">
<source type="video/ogg" src="http://media.w3.org/2010/05/bunny/movie.ogv">
<source type="video/mp4" src="http://media.w3.org/2010/05/bunny/movie.mp4">
</video>
<script>
var TEN_SECONDS = 10;
var video = document.getElementById("mejs_837308676760252_html5");
video.addEventListener('timeupdate', function() {
// don't have set the startTime yet? set it to our currentTime
if (!this._startTime) this._startTime = this.currentTime;
var playedTime = this.currentTime - this._startTime;
if (playedTime >= TEN_SECONDS) {
this.pause();
this.currentTime = 0;
}
});
video.addEventListener('seeking', function() {
// reset the timeStart
this._startTime = undefined;
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment