Created
January 29, 2012 09:45
-
-
Save html/1698060 to your computer and use it in GitHub Desktop.
Sequential scripts loading with jQuery
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
// 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$; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code is used in https://github.com/html/jquery-seq