0

I have some jQuery ajax requests which I am running in parallel with a .when call. I want to ignore the ones that haven't returned after their timeout period.

Here is a simple form of it:

var ajax1 = $.ajax({url: url1,
    timeout: 1000,
    success: function() {
        console.log("1 done");
    }
});

var ajax2 = $.ajax({url: url2,
    timeout: 1000,
    success: function() {
        console.log("2 done");
    }
});

$.when(ajax1, ajax2).done(function() {
    console.log("all done");
});

In the above example, if url1 doesn't respond without 1 second, I still want to reach the "all done". I know how to trap the timeout error in each ajax call, but I don't know how to flag the error as ignored.

explunit
  • 18,967
  • 6
  • 69
  • 94

1 Answers1

4

Can't you just use .always() instead of .done() if you want all ways (success, error, timeout, etc...) that the ajax call is finished instead of just successful ways it finishes:

$.when(ajax1, ajax2).always(function() {
    console.log("all done");
});

The arguments passed to the .always() callback can be used to determine success or type of error from the ajax calls.

jfriend00
  • 683,504
  • 96
  • 985
  • 979