`

ajax超时

阅读更多
var havereturn=false; 
function ontimeout(){ 
  if(!havereturn){ 
    //还没返回,
    alert("连接超时"); 
    havereturn=true; 


    //也许有什么办法让xmlhttp停下来,可以写在这,不停也没关系。 
        //window.clearTimeout(); 
      //ajax.abort(); 
    //ajax = null; 
    //delete ajax; 
  } 
} 

function doYourAjax(){//------你的ajax请求 
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP") 

xmlhttp.onreadystatechange=function(){ 
  if (xhr.readyState == 4) { 
    if (xhr.status == 200) { 
       if(!havereturn){ 
          //你处理返回结果的代码 
          var res = xmlhttp.responseText; 
          alert(res); 
       } 
    } 
    havereturn=true; 
  } 
} 

havereturn=false; 
xmlhttp.Open("POST", "www.iteye.com"); 
setTimeout("ontimeout()",5000);//5秒后执行ontimeout 
xmlhttp.Send(data); 
}//---------
分享到:
评论
2 楼 www88485400 2011-01-05  
这种方式是有问题的 同步请求的时候他是一个阻塞的 那个timeout也会中断时间已经起不到作用了。
1 楼 yangour 2009-10-29  
不行啊
断网后ie就死掉一样
执行不了阿
怎么在超时后结束xmlhtpp?

相关推荐

Global site tag (gtag.js) - Google Analytics