Trying to make a "queue manager", but I can't quite wrap my head around on how to accomplish this. I realize I can simply disable async
, but that blocks everything and I need some things to trigger.
My idea is to use callbacks or anything to signal the queue manager that you can let another item through. I have attempted it multiple times and just can't get it quite right.
This is how I imagine it:
mc.queueManager = function(ajaxRequest) {
var queue = []; // The "queue", an array
this.request = ajaxRequest;
queue.push(this.request);
this.curtask = queue.shift() // Grab and remove the first item from the queue
$.ajax({
type: "POST",
url: "handler.php",
data: {"data": this.curtask},
success: function(data) {
// Handle data retrieved
},
complete: function() {
// So, request completed, now this is the callback
if (queue.length > 0) {
mc.queueManager(queue.shift()); // Grab next item and repeat
}
}
});
};
Right now I can understand nothing is preventing multiple requests from firing, I can't figure out how to do that right now. I'm currently looking into $.deferred
in the documentation. I also tried a different method using $.when()
/ .then()
, but to no avail.