I know I can loop a video infinitely using the 'loop' attribute. But can I limit the number of times the video loops to perhaps 5 times?
As several comments mentioned, you can remove the loop attribute to get it to stop looping. You could keep it in the HTML code and then just remove it when you want to stop looping.
You will need to use JavaScript to achieve this. Have a look at the snippet below:
var iterations = 1;
document.getElementById('iteration').innerText = iterations;
myVideo.addEventListener('ended', function () {
if (iterations < 5) {
this.currentTime = 0;
this.play();
iterations ++;
document.getElementById('iteration').innerText = iterations;
}
}, false);
<div>Iteration: <span id="iteration"></span></div>
<video width="320" height="240" id="myVideo" controls>
<source src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" type="video/mp4" />
<source src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.ogv" type="video/ogg" />
Your browser does not support the video tag.
</video>
iterations
which will store our current iteration. We set this as 1
.myVideo
video which fires when the video ends.iterations
variable hasn't exceeded our number of plays which is 5
.currentTime
to 0
and then using play()
function to start the video.iterations
variable by 1
and the whole process starts again until our iterations
variable reaches 5
at which point it stops.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With