I need to load a variable number of javascript source files before running javascript code that depends on them. Sometimes 1 script needs to be loaded, other times 2. The ge
One way is to list all your scripts in an array, track how many scripts have loaded vs. the amount you want to load. Something like this:
var toLoad = ["1.js", "2.js", "3.js"], loaded = 0;
var onLoaded = function() {
loaded++;
if (loaded == toLoad.length) {
console.log('All scripts loaded!');
} else {
console.log('Not all scripts are loaded, waiting...');
}
}
for (var i = 0, len = toLoad.length; i < len; i++) {
$.getScript(toLoad[i], onLoaded);
}