ajax timeout callback function

后端 未结 1 844
醉梦人生
醉梦人生 2020-12-29 22:21

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          


        
相关标签:
1条回答
  • 2020-12-29 22:50
    $.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  
        }
    });
    
    0 讨论(0)
提交回复
热议问题