Skip to content

Instantly share code, notes, and snippets.

@SpartakusMd
Created August 5, 2015 11:08
Show Gist options
  • Save SpartakusMd/8e1872af48651f263f5c to your computer and use it in GitHub Desktop.
Save SpartakusMd/8e1872af48651f263f5c to your computer and use it in GitHub Desktop.
Run queued AJAX requests
$(function () {
//setup an array of AJAX options, each object is an index that will specify information for a single AJAX request
var ajaxes = [{ url : '<url>', dataType : 'json' }, { url : '<url2>', dataType : 'xml' }],
current = 0;
//declare your function to run AJAX requests
function do_ajax() {
//check to make sure there are more requests to make
if (current < ajaxes.length) {
//make the AJAX request with the given data from the `ajaxes` array of objects
$.ajax({
url : ajaxes[current].url,
dataType : ajaxes[current].dataType,
success : function (serverResponse) {
// ...
//increment the `current` counter and recursively call this function again
current++;
do_ajax();
}
});
}
}
//run the AJAX function for the first time once `document.ready` fires
do_ajax();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment