Here’s a way to use $.getScript()
to load multiple files using a counter:
function getScripts(list, fn) {
var cntr = list.length;
for (var i = 0; i < list.length; i++) {
$.getScript(list[i], function() {
--cntr;
if (cntr === 0) {
fn();
}
});
}
}
getScripts(["file1.js", "file2.js"], function() {
// all scripts loaded here
});
Here’s a way using promises:
function getScripts(list) {
var promises = [];
for (var i = 0; i < list.length; i++) {
promises.push($.getScript(list[i]));
}
return $.when.apply($, promises);
}
getScripts(["file1.js", "file2.js"]).then(function() {
// all scripts loaded here
});
You can use these same concepts for your CSS files.
solved Load files and then call a call back function [duplicate]