1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > jquery getJSON 中对超时Timeout的处理

jquery getJSON 中对超时Timeout的处理

时间:2018-12-04 19:50:16

相关推荐

jquery getJSON 中对超时Timeout的处理

调用jquery1.4 的getJSON()方法时,如下代码就能处理当URL为非法URL,或者URL繁忙而不能返回响应、超时等状况。代码源自 /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这种例外的一种必须的处理。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。