您好,欢迎访问一九零五行业门户网

AJAX请求队列使用详解

这次给大家带来ajax请求队列使用详解,使用ajax请求队列的注意事项有哪些,下面就是实战案例,一起来看一下。
ajax在使用的过程中会遇到一个问题,当用户短时间内执行了多个异步请求的时候,如果前一个请求没完成,将会被取消执行最新的一个请求,大多数情况下,不会有什么影响,例如请求了一个新的列表,旧的请求也就没什么必要了 ,但是,当我们的web程序需要同时异步调用多个请求,或者需要用户请求的是不同类型的数据,都需要执行完成的时候就出现问题 了,于是,将用户的请求记录下来,并按顺序执行。
不同的浏览器,允许同时执行的线程不同,通常ie允许两个线程,于是,当同时执行的异步请求超过两个时,就会变成只执行最新的两个。
ajax队列很简单,创建一个数组存储请求队列,数组中每一项又是一个请求参数数组,当用户执行请求时,不是直接执行ajax,首先将参数作为一个数组作为项再存入队列,检查队列中是否存在多个请求,如果没有,直接执行当前队列中这唯一的一项,如果有则不执行(因为有其他项,说明队列还在执行中,不必着急,其他项执行完了会轮到这一项的),ajax执行完成后就删除当前执行的队列项,然后再检查数组还有没有请求,有就继续执行到所有请求都完成为止,以下是我构建的一个队列,ajax部分是之前封装的。
//ajax function var reqobj; //creat null instence var requestarray = new array(); //var whichrequest; //加入请求队列 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);   //将当前请求添加到队列末尾     if(requestarray.length==1) //如果请求队列里只有当前请求立即要求执行队列,如果有其他请求,那么就不要求执行队列     {       exerequestarray();     } } //执行队列里的顺序第一个的请求 function exerequestarray() {   if( requestarray.length>0) //如果队列里有请求执行 ajax请求   {     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;   //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 '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 function function 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(); //移除队列里的顺序第一个的请求,即当前已经执行完成的请求     exerequestarray();   //要求执行队列中的请求   } }
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
ajax读取properties步骤详解
jquery+ajax提交form表单方法总结
以上就是ajax请求队列使用详解的详细内容。
其它类似信息

推荐信息