I have a button tied to a click handler -- which makes one or more AJAX calls, depending on how many items are in an array. I use the blockUI jquery plugin to show a message while the ajax call(s) are being made, then I show the results, and remove the blockUI message by calling $.unblockUI();.
Problem: For some reason, no matter how many times I want the ajaxCall function to execute, after the FIRST ajax call completes, the messaging is removed even though the loop still continues to execute, and the results display correctly in the #results div. I want the full number of loop iterations to complete before removing the messaging, but it seems like its not executing sequentially. Here is the code in question:
$("#myButton").live("click", function(){
$.blockUI({ message: '<img src="new_loader.gif" /><h2>Getting Config Files</h2><small>Please be patient.</small>',
css: {
border: 'none',
padding: '15px',
backgroundColor: '#FFF',
'-webkit-border-radius': '10px',
'-moz-border-radius': '10px',
color: '#000'
}
});
for (var i=0; i< myArray.length; i++)
{
ajaxCall(myArray[i]);
}
$("#results").show('slow');
$.unblockUI();
return false;
});
Is there an explanation for why the code AFTER the loop is executing even though the loop isn't complete yet? Any help it appreciated! Thanks.
is done asynchronous so the method is fired within the loop, but the execution will take a little more time, thus ending after the rest of the code is finished.
an nice way of dealing with this kind of behaviour is found in this article: Asynchronous method queue chaining in JavaScript