Ajax request queue Implementation _ajax related

Source: Internet
Author: User

Ajax will encounter a problem in the process of using, when a user executes multiple asynchronous requests within a short period of time, if the previous request is not completed, it will be canceled to execute the latest request, most of which will have no effect, such as requesting a new list, and the old request is not necessary, but When our web program needs to simultaneously invoke multiple requests asynchronously, or require different types of data to be requested by the user, there is a problem when the execution is done, and the user's request is recorded and executed sequentially.

Different browsers, which allow different threads to execute at the same time, typically IE allows two threads, so that when more than two asynchronous requests are executed at the same time, they become only the latest two.

Ajax queues are simple, creating an array to store request queues, and each item in the array is an array of request parameters, and when the user executes the request, instead of directly executing AJAX, the parameter is stored in the queue as an array, checking for multiple requests in the queue, and if not, Executes the only item directly in the current queue, if there is no execution (because there are other items, the queue is still in execution, there is no need to worry, the other items are finished the turn to this item), Ajax execution is completed, delete the currently executing queue items, and then check the array has no request, As soon as all the requests are completed, the following is a queue I built, and the Ajax part was previously encapsulated.

Ajax Function var reqobj;
creat Null instence var requestarray = new Array ();
var whichrequest;
    Join 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);
    Adds the current request to the end of the queue if (requestarray.length==1)//If only the current request in the request queue immediately requires execution of the queue, if there are other requests, then the execution queue {Exerequestarray () is not required; The order in the execution queue the first request function Exerequestarray () {if (requestarray.length>0)//If there is a request in the queue to perform an AJAX request {var argi  tem = 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;
  Whichrequest = Whichreq; if (window.
  XMLHttpRequest)//compatible Mozilla, Safari,...   {reqobj = new XMLHttpRequest ();    creat xmlhttprequest Instance if (reqobj.overridemimetype)//if Mime Type is false, then set MimeType ' Te
    Xt/xml ' {reqobj.overridemimetype (' text/xml '); ' Else if ' window. ActiveXObject)//compatible IE {try {reqobj = new ActiveXObject ("Msxml2.xmlhttp");//creat xmlhttpreques T Instance} catch (e) {try {reqobj = new ActiveXObject ("Microsoft.XMLHTTP");//creat XM Lhttprequest 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 function function Getrequest (callbackfun) {//judge readystate information if (reqobj.readystate = 4)
    {//judge status information if (Reqobj.status =) {eval (callbackfun+ "(reqobj)"); else {alert (' There was-a problem with the request. ') +reqobj.status+ "CallFunction:" +callbackfun); else alert warnning} requestarray.shift ();   Remove the order in the queue the first request, which is currently executing the completed request Exerequestarray ();
 Request executing requests in queue}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.