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?

0

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.