1

My page sends several HTTP requests in background. The requests are triggered by some user actions. Some user actions can cause redirect - and here we have a problem: we want redirect to happen only when all the requests were sent.

The question is: is there any Javascript library you can recommend that would allow to wait for all the reports to be sent and trigger redirect only after?

BreakPhreak
  • 9,960
  • 25
  • 69
  • 107

2 Answers2

0

With you can probably take advantage of ajaxStop event, which is triggered when all AJAX calls are done (no more pending connections). Here is an example:

$(document).ajaxStop(allDone);

function allDone() {
    console.info("Now redirecting");
}

$.getJSON('/echo/json/', function() {
    console.info("First done");
    $.getJSON('/echo/json/', function() {
        console.info("Third done");
    });
});
$.getJSON('/echo/json/', function() {
    console.info("Second done");
});

No matter what will be the order of responses, the allDone() callback is always executed last.

Tomasz Nurkiewicz
  • 324,247
  • 67
  • 682
  • 662
0

You want to use jQuery Deferred pattern which allows you to chain (AJAX) objects and

  • have success callback if all complete succesfully

  • have fail callback if any of the requests fails

http://api.jquery.com/category/deferred-object/

Example:

How to chain ajax calls using jquery

Community
  • 1
  • 1
Mikko Ohtamaa
  • 76,495
  • 46
  • 227
  • 378