I need to run some ajax requests on the server and then start the callback when they are complete. Normally this would be easy with jQuery deferred.done(). However, in order to avoid server suppression, I execute a request queue and start every millisecond every time.
eg
var promisesList = [];
var addToQueue = function(workflow) {
workflowQueue.push(workflow);
}
var startWorkflow = function(workflow) {
return $.ajax($endointURL, {
type: "POST",
data: {
action: workflow.id
},
success: function() {
},
error: function(jqXHR, textStatus, errorThrown) {
}
});
};
var startWorkflows = function() {
var promisesList = [];
if (workflowQueue.length > 0) {
var workflow = workflowQueue.shift();
promisesList.push(startWorkflow(workflow));
setTimeout(startWorkflows, delay);
}
};
startWorkflows();
$.when(promisesList).done(function(){
});
The problem is that the array is promisesListinitially empty, so the callback is done()triggered immediately, and then ajax requests start to get sent setTimeout(). Is there an easy way to create ajax requests initially and sort of “pause” and then run them using setTimeout().
/, ajax- , , , .