आप रिकर्सिव फ़ंक्शन कॉल के रूप में $.getScript()
के कॉलबैक फ़ंक्शन का उपयोग कर लोडिंग समाप्त होने के बाद प्रत्येक को लोड कर सकते हैं।
//setup array of scripts and an index to keep track of where we are in the process
var scripts = ['script1.js','script2.js','script3.js'],
index = 0;
//setup a function that loads a single script
function load_script() {
//make sure the current index is still a part of the array
if (index < scripts.length) {
//get the script at the current index
$.getScript(scripts[index], function() {
//once the script is loaded, increase the index and attempt to load the next script
console.log('Loaded: ' + scripts[index]);
index++;
load_script();
});
}
}
क्या अपने कोड में होने वाली है कि स्क्रिप्ट एक ही समय में अनुरोध किया जा रहा है और जब से वे अतुल्यकालिक रूप से लोड करते हैं, वे लौट सकते हैं और यादृच्छिक क्रम में निष्पादित।
अद्यतन
मैं इस परीक्षण नहीं किया है, लेकिन अगर स्क्रिप्ट स्थानीय स्तर पर होस्ट किया गया है, तो आप तो, सादे पाठ में उन्हें पुनः प्राप्त जब तक वे सभी जिस पर लोड किए गए हैं चर में कोड के सभी स्टोर करने के लिए कोशिश कर सकते बार जब आप क्रम में स्क्रिप्ट का मूल्यांकन कर सकते हैं:
var scripts = ['script1.js','script2.js','script3.js'],
//setup object to store results of AJAX requests
responses = {};
//create function that evaluates each response in order
function eval_scripts() {
for (var i = 0, len = scripts.length; i < len; i++) {
eval(responses[scripts[i]]);
}
}
$.each(scripts, function (index, value) {
$.ajax({
url : scripts[index],
//force the dataType to be `text` rather than `script`
dataType : 'text',
success : function (textScript) {
//add the response to the `responses` object
responses[value] = textScript;
//check if the `responses` object has the same length as the `scripts` array,
//if so then evaluate the scripts
if (responses.length === scripts.length) { eval_scripts(); }
},
error : function (jqXHR, textStatus, errorThrown) { /*don't forget to handle errors*/ }
});
});
आह, निंजा। हालांकि अच्छा काम है। –
ऐसा लगता है कि काम कर रहा है !, धन्यवाद और बहुत अच्छी व्याख्या! – greenbandit