I have two arrays.
Any items in (2) may be dependant on any items in (1). Meaning that I have to be sure that all items in (1) have finished loading before any of (2) are registered.
I want to load all of (1) asynchronously... since it will be faster, but how do I ensure that all of these processes have finished before registering (2)?
I think this way it should work:
var scripts = [
{"src": "script1", "loaded": false},
{"src": "script2", "loaded": false},
{"src": "script3", "loaded": false},
]
var commands = ["cmd1","cmd2","cmd3"];
for (var i = 0, l = scripts.length; i<l; i++){
(function (script){
$.ajax({
url: script.src,
dataType: 'script',
success: function (){
for (var k = scripts.length; k--;){
if (scripts[k].src === script.src){
scripts[k].loaded = true;
}
}
var allReady = true;
for (var k = scripts.length; k--;){
if (!scripts[k].loaded){
allReady = false;
}
}
if (allReady){
/*execute your inline commands*/
}
}
});
})(scripts[i]);
}
This should in my opinion be solvable via jQuery's callback capacity; see succinct usage descriptions here , hope this helps.
EDIT: here's the actual code:
$.extend({myFunc : function(someArg, callbackFnk){
// load files here
var data = 'test';
// now call function for inline loading
if(typeof callbackFnk == 'function'){
callbackFnk.call(this, data);
}}});
$.myFunc(someArg, function(arg){ */here goes inline load*/ });
你可以在ajax调用的成功处理程序中添加(2)。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.