當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
jquery getJSON 中对超时Timeout的处理
生活随笔
收集整理的這篇文章主要介紹了
jquery getJSON 中对超时Timeout的处理
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
調(diào)用 jquery1.4 的getJSON()方法時(shí),如下代碼就能處理當(dāng)URL為非法URL,或者URL繁忙而不能返回響應(yīng)、超時(shí)等狀況。代碼源自 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');
? ? ? ? ? ? }
? ? ? ? }
? ? }
以上寫法應(yīng)該成為調(diào)用getJSON的時(shí)候標(biāo)準(zhǔn)模式,即對(duì)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');
? ? ? ? ? ? }
? ? ? ? }
? ? }
以上寫法應(yīng)該成為調(diào)用getJSON的時(shí)候標(biāo)準(zhǔn)模式,即對(duì)timeout這種例外的一種必須的處理。
轉(zhuǎn)載于:https://www.cnblogs.com/youcanwin/archive/2012/04/10/2440411.html
總結(jié)
以上是生活随笔為你收集整理的jquery getJSON 中对超时Timeout的处理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【入门经典】创建站点地图
- 下一篇: gvim在windows下中文乱码的终极