Javascript DOM ontimeupdate Event via addEventListener() method

Introduction

In JavaScript, using the addEventListener() method:

object.addEventListener("timeupdate",
       myScript);

In this example, we use the addEventListener() method to attach a "timeupdate" event to a video element.

When the user starts to play the video, or skips to a new position in the video, a function is triggered.

It will display the current position (in seconds) of the video playback.

View in separate window

<!DOCTYPE html>
<html>
<body>
<video id="myVideo" controls>
  <source src="video.mp4" type="video/mp4">
  <source src="video.ogg" type="video/ogg">
  Your browser does not support HTML5 video.
</video>/*from   www  . j a  va 2 s.c o m*/

<p>Playback position: <span id="demo"></span></p>

<script>
// Get the video element with id="myVideo"
var x = document.getElementById("myVideo");

x.addEventListener("timeupdate", myFunction);

function myFunction() {
  document.getElementById("demo").innerHTML = x.currentTime;
}
</script>

</body>
</html>
Bubbles: No
Cancelable: No
Event type: Event
Supported HTML tags: <audio> and <video>



PreviousNext

Related