You're getting this because $.getScript is an asynchronous method. In this case, it means that the method returns immediately before the script has finished loading, and continues executing code after it.
What this means is that something like:
$.getScript('a.js', function () {
console.log('Loaded a');
});
$.getScript('b.js', function () {
console.log('Loaded b');
});
$.getScript('c.js', function () {
console.log('Loaded c');
});
// Output could be:
// Loaded c
// Loaded a
// Loaded b
This means that all of the script files requests can be done simultaneously but it also means that the order is not deterministic (fixed).
Use Promises
You can ensure that the getWidgetContent
is called sequentially by chaining promises if you are using jQuery 1.5 and above.
However, the pitfall of this method is that you will not be concurrently loading all the script requests at the same time, the requests will be sent one after another after the previous one has completed.
Make sure you return the result of $.getScript which is a Deferred Object (I made minimal changes here, just note the return
statements):
function getWidgetContent( widget ) {
if(widget.script!=null){
$global_widget_id = widget.widget_id;
return $.getScript( "js/" + widget.script, function() {
$( ".widget_header_title_" + widget.widget_id ).append( widget.title );
});
}
return null;
}
New method to perform a new getWidgetContent after a previous promise is fulfilled (completion of previous action):
function doGetWidgetContentAfter(promise, widget) {
return promise.then(function () {
return getWidgetContent( widget );
});
}
Calling it:
var promise = $.when(true);
for ( j = 0; j <= widget_data.d.length - 1; j++ ) {
promise = doGetWidgetContentAfter( promise, widget_data.d[j] );
}
Explanation
What doGetWidgetContentAfter
does is that when the said promise
is complete then call the getWidgetContent
function for widget
. The then
method returns a promise that completes when any internal methods promise completes.
I know it's starting to sound complicated, but do play around and experiment with it. :)