There is nothing complicated about using an array object to do the queue and maintain the order of the Ajax requests. The following code is given:
Copy Code code as follows:
;(function ($) {
$.ajaxqueue = {
Queue to manage AJAX requests
Requests:new Array (),
To join a queue for an AJAX request to be sent
Offer:function (options) {
var _self = this,
"Hijack" the Complete,beforesend method and join the queue processing method poll
Xhroptions = $.extend ({}, options, {
If the request completes, send the next request
Complete:function (JQXHR, Textstatus) {
if (Options.complete)
Options.complete.call (This, jqxhr, textstatus);
_self.poll ();
},
If the request is canceled, continue sending the next request
Beforesend:function (JQXHR, settings) {
if (options.beforesend)
var ret = Options.beforeSend.call (this, jqxhr, settings);
if (ret = false) {
_self.poll ();
return ret;
}
}
});
This.requests.push (xhroptions);
if (this.requests.length = = 1) {
$.ajax (xhroptions);
}
},
To send AJAX requests in FIFO mode
Poll:function () {
if (This.isempty ()) {
return null;
}
var processedrequest = This.requests.shift ();
var nextrequest = This.peek ();
if (nextrequest!= null) {
$.ajax (nextrequest);
}
return processedrequest;
},
Return the AJAX request for the queue header
Peek:function () {
if (This.isempty ()) {
return null;
}
var nextrequest = this.requests[0];
return nextrequest;
},
To determine whether a queue is empty
Isempty:function () {
return this.requests.length = = 0;
}
}
}) (JQuery);
The use of the words is $.ajaxqueue.offer (settings), settings and the configuration of the jquery document consistent.
If you are interested, you can click on my jsfiddle share to run online, modify and so on. Finally, what is the problem, welcome to exchange: