Last active
August 29, 2015 14:15
-
-
Save kakawajazz/13353d1a532abb4860bb to your computer and use it in GitHub Desktop.
Preload HTML5 video with JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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