Javascript DOM HTML Video currentTime property set the current playback position to 5 seconds

Introduction

Using the currentTime property to set the current playback position to 5 seconds:

document.getElementById("myVideo").currentTime = 5;

In this example, we attach a "timeupdate" event to a video element.

The currentTime property is used to get the current position of the audio/video playback.

View in separate window

<!DOCTYPE html>
<html>
<body>
<video id="myVideo" width="100" height="100" controls>
  <source src="video.mp4" type="video/mp4">
  <source src="video.ogg" type="video/ogg">
  Your browser does not support the video tag.
</video><br>

<button onclick="setCurTime()" type="button">Set time position to 5 seconds</button>
<p id="demo"></p>

<script>
// Get the video element with id="myVideo"
var x = document.getElementById("myVideo");
// Attach a "timeupdate" event to the video
x.addEventListener("timeupdate", getCurTime);
function getCurTime() {/*from w  ww.  jav  a  2 s. c o m*/
  document.getElementById("demo").innerHTML = "The current playback position is " + x.currentTime + " seconds.";
}
// Set the current playback position to 5 seconds
function setCurTime() {
  x.currentTime = 5;
}
</script>

</body>
</html>



PreviousNext

Related