阿贾克斯超时回调函数回调、函数、阿贾克斯

2023-09-10 16:38:12 作者:赴火

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

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

});
 

解决方案

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

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

教程 每天WPS打卡签到领取10天 会员 腾讯云函数Python脚本

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  
    }
});