的getJSON超时处理 [英] getJSON timeout handling

查看:194
本文介绍了的getJSON超时处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery 的getJSON()功能。该功能获取的数据没有问题。但有时等待,等待......等待我的加载条显示在页面的中心加载载荷loadin。  因此,jQuery的阿贾克斯()函数有一个超时变量。但我想用的getJSON 功能。我认为,我可以使用 ajaxStart() ajaxStop()功能。但如何?

  $('。loadingDiv)
    。隐藏()
    .ajaxStart(函数(){
        $(本).fadeIn();
        的setTimeout(扔'';,15000)//我用这个,但没有奏效
        的setTimeout(返回;,15000)//我用这个,但没有奏效
        的setTimeout(中止();,15000)//我用这个,但没有工作(中止所有的Ajax事件)。
    })
    .ajaxStop(函数(){
        $(本).fadeOut();
    });
 

解决方案

的getJSON()返回的承诺上,您可以拨打中止功能:

 变种P = $ .getJSON(...,函数(){警报(成功);});
的setTimeout(函数(){p.abort();},2000);
 

编辑:但如果你的目标只是中止如果花费太多的时间,那么杀伤力吉他的回答比较好

I am using jQuery getJSON() function. This function getting data with no problem. But sometimes waiting, waiting waiting... And my loading bar showing loading loading loadin at center of page. So jQuery ajax() function have an timeout variable. But i want to use getJSON function. And i think that i can use ajaxStart() and ajaxStop() functions. But how?

$('.loadingDiv')
    .hide()
    .ajaxStart(function() {
        $(this).fadeIn();
        setTimeout("throw '';",15000) //i used this but didn't work
        setTimeout("return;",15000) //i used this but didn't work
        setTimeout("abort();",15000) //i used this but didn't work.(Abort all ajax events)
    })
    .ajaxStop(function() {
        $(this).fadeOut();
    });

解决方案

getJSON() returns a promise on which you can call the abort function :

var p = $.getJSON(..., function(){ alert('success');});
setTimeout(function(){ p.abort(); }, 2000);

EDIT : but if your goal is just to abort if it takes too much time, then lethal-guitar's answer is better.

这篇关于的getJSON超时处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆