Async AJAX through an array
by tonytlwu
HTML
<div id="debug"></div>
JavaScript
var inputList = [ 10, 20, 30, 40, 50, 60 ];
function mockAjax (input, callback){
$('#debug').append('Sending request with input: ' + input + '<br>');
setTimeout(function(){
$('#debug').append('Request with input: ' + input + ' completed.<br>');
if (typeof(callback) === 'function') {
callback();
}
},1000);
/*
Include:
- Calling to server using the input provided
- Run the callback function as provided to the function
*/
}
function sendRequest (){
if (inputList.length) {
var input = inputList.shift();
mockAjax(input,sendRequest);
}
}
sendRequest();