欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > javascript >内容正文

javascript

jquery getJSON 中对超时Timeout的处理

发布时间:2025/4/16 javascript 47 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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

总结

以上是生活随笔为你收集整理的jquery getJSON 中对超时Timeout的处理的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。