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
  • 10,940
  • 26
  • 72
  • 108

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
  • 334,321
  • 69
  • 703
  • 674
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
  • 82,057
  • 50
  • 264
  • 435