Jquer ajaxqueue Simple Implementation Code _jquery

Source: Internet
Author: User
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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.