Quantcast
Viewing all articles
Browse latest Browse all 7

Answer by Chris Moschini for Tell whether video is loaded or not in Javascript

It's pretty easy for your script to load AFTER the video, if the video is cached. That can make listening to these events unreliable. I resolved this race condition like so:

(() => {  const video = $('#video');  const onVideoLoaded = () => {    ...  };  if (video[0].duration > 0)    onVideoLoaded();  else    video.on('loadedmetadata', onVideoLoaded);})();

This will at least get you to solid ground - if the video loads, your method will always fire exactly once - whether the video loads before the script, or the script before the video. Attaching to the event naively is going to get you code that mysteriously does nothing on cached visits.


Viewing all articles
Browse latest Browse all 7

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>