Skip to content

Instantly share code, notes, and snippets.

@ezmiller
Created June 23, 2015 13:21
Show Gist options
  • Save ezmiller/1a4a364fa167b90ed133 to your computer and use it in GitHub Desktop.
Save ezmiller/1a4a364fa167b90ed133 to your computer and use it in GitHub Desktop.
A example method for preloading html5 videos in a queue
// Have to use an object so that count can function as a reference
var loadQueue = {
count:0
}
/**
* Preloads videos
*
* If limit has been reached video is not preloaded but an
* interval is set that checks to see if the queue count
* is below the limit. For this to work a 'canplaythrough'
* event needs to deprecate the loadQueue.count when it is
* fired.
* /
var preload = function(players) {
var limit;
// Max videos that can preload at once.
limit = 2;
function callback(video, queue, interval) {
// console.log('interval callback, queue:', queue, interval);
if (queue.count < limit) {
console.log('queueing: ', video);
queue.count++;
player.preload = true;
clearInterval(interval);
}
}
players.map(function(player) {
var interval = setInterval(function() {
callback(video, loadQueue, interval);
}.bind(this), 1000);
}.bind(this));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment