javascript – How to run a function when a html5 video starts playing

javascript – How to run a function when a html5 video starts playing

var vid = document.getElementById(myVideo);
vid.onplay = function() {
    alert(The video has started to play);
};

From here: https://www.w3schools.com/tags/av_event_play.asp

Try this, play returns a promise.

const video = document.getElementById(myVideo);

async function playVideo() {
  try {
    await video.play();
    // video is playing, do your stuff here
  } catch(err) {
    // video is not playing
  }
}

if(video.paused) {
    playVideo()
 }

For more info: https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/play

javascript – How to run a function when a html5 video starts playing

Leave a Reply

Your email address will not be published.