Skip to content

Instantly share code, notes, and snippets.

@charlesjolley
Created September 8, 2010 04:38
Show Gist options
  • Save charlesjolley/569633 to your computer and use it in GitHub Desktop.
Save charlesjolley/569633 to your computer and use it in GitHub Desktop.
var queue = [];
var scripts ={};
function loadIt(scriptname) {
queue.push(scriptname);
if (!scripts[scriptname]) {
var el = document.createElement('script');
el.src = scriptname;
document.head.appendElement(el);
} else pump();
}
function fullfill(scriptname, script) {
scripts[scriptname] = script;
pump();
}
function pump() {
if (queue.length === 0) return; // nothing to do
var name = queue[0];
if (scripts[name]) {
queue.shift(); // remove item
eval(scripts[name]);
setTimeout(pump, 1); // try to pump again. take a break to avoid slow scripts
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment