阿贾克斯超时回调函数 [英] ajax timeout callback function

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

问题描述

有没有办法运行功能,如果jQuery的$就功能击中它的暂停

  $。阿贾克斯({
...
...
,超时:1000(){做一些事情,如果超时)
...

});
 

解决方案

  $。阿贾克斯({
    ...
    超时:1000,
    错误:函数(jqXHR,textStatus,errorThrown){
        如果(textStatus ===超时){
           //做一些对超时
        }
    }
});
 

有关更多信息,请查阅jQuery的文档:

http://api.jquery.com/jQuery.ajax/


编辑

它已经一年多,因为我最初回答这个和 textStatus 可能值已更改为成功,notmodified,错误,暂停,中止,parsererror。对于错误回调,只有最后四种状态是可能的。

另外,您现在可以通过返回的JQuery延期承诺对象的 .fail 方法连接你的错误处理程序:

  VAR承诺= $阿贾克斯({超时:1000});

promise.fail(功能(jqXHR,textStatus){
    如果(textStatus ===超时){
        //处理超时
    }
});
 

Is there a way to run a function if jQuery's $.ajax function hits it's timeout?

i.e.

$.ajax({
...
...
,timeout:1000(){do something if timeout)
...

});

解决方案

$.ajax({
    ...
    timeout: 1000,
    error: function(jqXHR, textStatus, errorThrown) {
        if(textStatus==="timeout") {
           //do something on timeout
        } 
    }
});​

For more information check out the jQuery documentation:

http://api.jquery.com/jQuery.ajax/


Edited

It's been over a year since I initially answered this and the textStatus possible values have changed to "success", "notmodified", "error", "timeout", "abort",or"parsererror". For error callbacks, only the last four statuses are possible.

Also you can now wire your error handlers through the returned JQuery deferred promise object's .fail method:

var promise = $.ajax({ timeout: 1000 });

promise.fail(function(jqXHR, textStatus) {
    if(textStatus==="timeout") {
        // handle timeout  
    }
});

这篇关于阿贾克斯超时回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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