`
ihyperwin
  • 浏览: 426565 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

jquery ajax的timeout参数和js的window.setTimeout方法

 
阅读更多
程序要实现的功能,ajax访问服务器成功,但服务器繁忙,(在服务器上通过Thread.sleep(10000000)方法模拟服务器阻塞),如果1分钟没有响应,前台页面做响应的处理。我试了两种方法。
1.jQuery  ajax timeout参数

$.ajax({
    async:false,
    cache:false,
    timeout:8000,
    type:"POST",
    url:"someurl.htm",
    data:allFormValues,
    error:function(jqXHR, textStatus, errorThrown){
         alert("some error occurred")
         alert(textStatus);
    },
    success:function(msg){ alert(msg); }
   });

效果:ajax成功访问服务器,一直处于服务器响应中状态,用fireBug观看XHR status状态码无,一直请求中。 n久之后,返回响应,进入success. 如果把服务器down掉,进入error方法。
这些不是我想要的结果。
timeout参数api如下:
timeout

Set a timeout (in milliseconds) for the request. This will override any global timeout set with $.ajaxSetup(). The timeout period starts at the point the $.ajax call is made; if several other requests are in progress and the browser has no connections available, it is possible for a request to time out before it can be sent. In jQuery 1.4.x and below, the XMLHttpRequest object will be in an invalid state if the request times out; accessing any object members may throw an exception. In Firefox 3.0+ only, script and JSONP requests cannot be cancelled by a timeout; the script will run even if it arrives after the timeout period.

The timeout period starts at the point the $.ajax call is made; if several other requests are in progress and the browser has no connections available, it is possible for a request to time out before it can be sent。
大概意思:如果有其他请求队列(该请求在客户端被阻塞),或者没有联网,就有可能在ajax sent方法被调用之前超时。   按照其定义,timeout参数,应该只是请求超时,不涉及响应。 此处,服务器被阻塞,请求却是成功了,服务器响应中。。。。。
而且,不会执行 error和success方法,直至响应完毕,或者服务器down掉(也算响应完毕)


2.window.setTimeout方法
本来用此法,思路如下:

function click(){


var  flag=0;

setTimeout(aa,5000);
function aa(){
  //to do something
   if(flag==0) {
       alert("timeout");
       window.reload();
    }
}

$.ajax({
    async;false,
    cache:false,
    timeout:8000,
    type:"POST",
    url:"someurl.htm",
    data:allFormValues,
    error:function(jqXHR, textStatus, errorThrown){
        flag=1;
        alert("some error occurred")
         alert(textStatus);
    },
    success:function(msg){ 
       flag=1;
      alert(msg);
   }
   });

}
预期效果:给ajax 5秒的时间,如果没响应(包括success和error),则做进一步处理。弹出timeout,刷新本页,干掉ajax. 
在网上查找  setTimeout方法确实是模拟了另开一个线程(不是真的?求解)
但效果:

setTimeout 5秒钟后并没有执行aa函数, 而是。。

ajax无限等待 response,等n久后或故意把服务端down掉,先进入ajax error回调或success回调函数, 又过5秒,在执行aa

晕了,为什么?是把整个function执行完,在执行setTimeout?

坐等各位遇到此问题的朋友来交流,大牛们给予指点。

问题已解决:把 $.ajax({
    async:false,
    cache:false,
    timeout:8000,
    type:"POST",
    url:"someurl.htm",
    data:allFormValues,
    error:function(jqXHR, textStatus, errorThrown){
         alert("some error occurred")
         alert(textStatus);
    },
    success:function(msg){ alert(msg); }
   });
设为 async:true,设为异步,
  可以了,8秒后返回弹出"some error occurred",then alert "timeout"

   参数设置为同步,timeout参数貌似不管用............
 
 







分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics