5

Lets look at my code:

$("#senderr").on("click", function(){
   $.get("/echo/json").then(function(res){
      //let's imagine something happen
      throw "error";
   });
});

$("#sendok").on("click", function(){
    $.get("/echo/json").then(function(res){
        $("#log").append("<li>send ok</li>");
    });
});

$(document).on("ajaxStart", function(){
    $("#log").append("<li>start</li>");
});

$(document).on("ajaxStop", function(){
    $("#log").append("<li>stop</li>");
});

JSFiddle

If you press "send ok", you can see that ajaxStart and ajaxStop events fired successfully. But if you just once press "send err" button which thrown runtime error in ajax callback, previous "send ok" behavior will never work again - ajaxStop event stop firing.

Why jQuery do it? What can i do to prevent this except using try-catch everywhere?

Stefan Becker
  • 5,695
  • 9
  • 20
  • 30
huston007
  • 1,972
  • 1
  • 16
  • 13

1 Answers1

4

That's what worked for me:

jQuery(window).on('error', function (e) {    
    // This tells jQuery no more ajax Requests are active
    // (this way Global start/stop is triggered again on next Request)
    jQuery.active--;
    //Do something to handle the error    
});
  • Thanks, that helped me a lot - I had to stop jquery BlockUI from working and here was the right place to add `$.unblockUI()` – leole Aug 08 '18 at 12:46