Skip to content

Instantly share code, notes, and snippets.

@kakawajazz
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kakawajazz/13353d1a532abb4860bb to your computer and use it in GitHub Desktop.
Save kakawajazz/13353d1a532abb4860bb to your computer and use it in GitHub Desktop.
Preload HTML5 video with JavaScript
function progressHandler() {
if (video.duration) {
var percent = (video.buffered.end(0) / video.duration) * 100;
if (percent >= 100) {
video.currentTime = 0;
console.log('video loaded');
return true;
}
video.currentTime++;
}
}
function addSourceToVideo(video, src, type) {
var source = document.createElement('source');
source.src = src;
source.type = type;
video.appendChild(source);
video.load();
}
console.log('video loading');
addSourceToVideo(video, $(video).data('src'), "video/mp4");
video.addEventListener('progress', progressHandler, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment