How to tell if a video has part-played

I have a page of videos and am trying to tell if the vid has been played and update the play count. The following (with the PHP script) works fine if, and only if, the vid plays right to the end.

const vid = document.getElementsByTagName("video")[0];
vid.addEventListener("ended", function(e) {
  console.log("Ended");
  fetch("updVideoCount.php?id=<?=$id?>");
});

If someone watches the vid but leaves the page during the credits, the count is not updated.

Is there any way to check if the video is played for, say, a minute?

Probably this… https://www.w3schools.com/tags/av_event_timeupdate.asp

And events list complete… https://www.w3schools.com/tags/ref_av_dom.asp, if link above helps not.

1 Like

Thanks @igor_g

vid.addEventListener("timeupdate", updateVc);
function updateVc () {
  if (vid.currentTime > 60) {
    console.log(vid.currentTime);
    fetch("updVideoCount.php?id=<?=$id?>");
    vid.removeEventListener("timeupdate", updateVc);
  }
}

seems to do the trick.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.