jquery getJSON 中对超时Timeout的处理

调用 jquery1.4 的getJSON()方法时,如下代码就能处理当URL为非法URL,或者URL繁忙而不能返回响应、超时等状况。代码源自 http://stackoverflow.com/questions/4138470/jquery-getjson-with-timeout

function
testAjax(){
       
varparams="test=123";
       
var isneedtoKillAjax =true;// set this true

       
// Fire the checkajaxkill method after 10 seonds
        setTimeout
(function(){
            checkajaxkill
();
       
},10000);// 10 seconds                

       
// For testing purpose set the sleep for 12 seconds in php page

$.getJSON('index2.php',params,function(data, textStatus){              
            isneedtoKillAjax
=false;// set to false
           
// Do your actions based on result (data OR textStatus)
       
});

       
function checkajaxkill(){

           
// Check isneedtoKillAjax is true or false,
           
// if true abort the getJsonRequest

           
if(isneedtoKillAjax){
                //myAjaxCall
.abort();
                alert
('killing the ajax call');                
           
}else{
                alert
('no need to kill ajax');
           
}
       
}
   
}

以上写法应该成为调用getJSON的时候标准模式,即对timeout这种例外的一种必须的处理。

转载于:https://www.cnblogs.com/youcanwin/archive/2012/04/10/2440411.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值