Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i continuously get a html-videos current time?

I’m trying in js to get a videos current time. I have come up with a rather ugly and not so precise solution, using a setInterval on the play event. But i would very much prefer using a better method. Maybe requestAnimationFrame could do it? I’m open for a better solution.

Current code:

  this.video = document.createElement('video')
  this.duration = null
  this.currentTime = null

  this.video.addEventListener('play', this.play)

  play(e) {
    setInterval(() => {
      this.currentTime = this.video.currentTime
    }, 1000)
  }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I think what you’re looking for is the timeupdate event.

Example:

let video = document.getElementById("video");
let currentTime = null;

video.addEventListener('timeupdate', timeUpdate);

function timeUpdate(e) {
  currentTime = e.target.currentTime;
  console.log("Time: " + currentTime);
}
<video id="video" width="400" controls>
  <source src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" type="video/mp4">
</video>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading