Skip to content

Instantly share code, notes, and snippets.

@html
Created January 29, 2012 09:45
Show Gist options
  • Save html/1698060 to your computer and use it in GitHub Desktop.
Save html/1698060 to your computer and use it in GitHub Desktop.
Sequential scripts loading with jQuery
// This snippet depends on https://gist.github.com/ccbeea00dd952bbe7eb4
function getScripts(collection, endcallback){
if(collection.length == 0){
return endcallback && endcallback();
}
jQuery.getScript(collection[0], function(){
getScripts(collection.slice(1), endcallback);
});
}
function getScriptsAdvanced(collection, endcallback){
if(!this.scriptsLoaded){
this.scriptsLoaded = [];
}
if(collection.length == 0){
return endcallback && endcallback();
}
var script = collection[0];
var callback = function(){
getScriptsAdvanced(collection.slice(1), endcallback);
};
if(this.scriptsLoaded.indexOf(script) == -1){
this.scriptsLoaded.push(script);
jQuery.getScript(script, callback);
}else{
callback();
}
}
function withScripts(){
var old$ = window.$;
window.$ = jQuery;
var scripts = Array.prototype.slice.call(arguments, 0, arguments.length - 1);
var endcallback = arguments[arguments.length - 1];
getScriptsAdvanced(scripts, function(){
endcallback();
window.$ = old$;
});
}
@html
Copy link
Author

html commented Aug 27, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment