Skip to content

Instantly share code, notes, and snippets.

@kakawajazz kakawajazz/load_video.js
Last active Aug 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.