AJAX Request queue implementation, ajax Request queue

Source: Internet
Author: User

AJAX Request queue implementation, ajax Request queue

When you execute multiple asynchronous requests in a short time, if the previous request is not completed, will be canceled to execute the latest request. In most cases, there will be no impact. For example, if a new list is requested, the old request will be unnecessary. However, when our WEB program needs to call multiple requests asynchronously at the same time or the user needs to request different types of data, the problem occurs when the execution is completed. Therefore, record user requests and execute them in sequence.

Different browsers allow different threads to be executed at the same time. Generally, Internet Explorer allows two threads. Therefore, when more than two asynchronous requests are executed at the same time, only the latest two requests are executed.

AJAX queue is simple. Create an array to store the Request queue. Each item in the array is an array of Request Parameters. When a user executes a request, it does not directly execute AJAX, first, the parameter is used as an array and then stored in the queue. check whether there are multiple requests in the queue. If not, directly execute this unique item in the current queue, if yes, do not execute (because there are other items, it indicates that the queue is still in progress and you don't have to worry about it. If the other items are executed, it will be your turn ), after AJAX is executed, it deletes the queue items currently executed, and then checks whether there are any requests in the array. If yes, it continues until all requests are completed. Below is a queue I have built, the AJAX part is previously encapsulated.

// Ajax Functionvar reqObj; // Creat Null Instencevar RequestArray = new Array (); // var whichRequest; // Add the Request queue function AddRequestArray (url, isAsy, method, parStr, callBackFun) {var ArgItem = new Array (); ArgItem [0] = url; ArgItem [1] = isAsy; ArgItem [2] = method; ArgItem [3] = parStr; argItem [4] = callBackFun; RequestArray. push (ArgItem); // Add the current request to the end of the queue if (RequestArray. length = 1) // if only the current request in the Request queue needs to be executed immediately, other requests are not required Row queue {ExeRequestArray () ;}// the first request function ExeRequestArray () {if (RequestArray. length> 0) // if a request in the queue executes an AJAX request {var ArgItem = RequestArray [0]; DoRequest (ArgItem [0], ArgItem [1], ArgItem [2], argItem [3], ArgItem [4]) ;}// Run Ajax (string urladdress, bool IsAsy, string method, string parameters, string whichRequest) function DoRequest (url, isAsy, method, parStr, callBackFun) {reqObj = false; // whichReques T = whichReq; if (window. XMLHttpRequest) // compatible Mozilla, Safari ,... {reqObj = new XMLHttpRequest (); // Creat XMLHttpRequest Instance if (reqObj. overrideMimeType) // if Mime Type is false, then set MimeType 'text/xml' {reqObj. overrideMimeType ('text/xml');} else if (window. activeXObject) // compatible IE {try {reqObj = new ActiveXObject ("Msxml2.XMLHTTP"); // Creat XMLHttpRequest Instance} Catch (e) {try {reqObj = new ActiveXObject ("Microsoft. XMLHTTP "); // Creat XMLHttpRequest Instance} catch (e) {}}// if reqObj is false, then alert warnning if (! ReqObj) {alert ('Giving up :( Cannot create an XMLHTTP instance'); return false;} reqObj. onreadystatechange = function () {GetRequest (callBackFun)}; // set onreadystatechange Function reqObj. open (method, url, isAsy); // set open Function reqObj. setRequestHeader ('content-type', 'application/x-www-form-urlencoded'); // set RequestHeader reqObj. send (parStr); // do send and send parameters} // get Service Response information Functionfunction GetRequest (callBackFun) {// judge readystate information if (reqObj. readyState = 4) {// judge status information if (reqObj. status = 200) {eval (callBackFun + "(reqObj)");} else {alert ('There was a problem with the request. '+ reqObj. status + "CallFunction:" + callBackFun); // else alert warnning} RequestArray. shift (); // remove the first request in the queue, that is, the current completed request ExeRequestArray (); // requests in the execution queue }}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.