Willem Ellis
Willem Ellis

Reputation: 5016

ajaxStop() doesn't fire

This is the code that wasn't working:

$(document).ajaxStop(function() {
    $(this).unbind("ajaxStop"); //prevent running again when other calls finish

    // Display everything
    display();
});

And here's my Ajax function:

function getAjax(url, callback) {
    jQuery.ajaxPrefilter(function( options ) {
        options.global = true;
    });

    $.ajax({
        url: url,
        type: "GET",
        dataType: "jsonp",
        success: callback
    });
}

Why does ajaxStop() never fire?

Upvotes: 4

Views: 1740

Answers (1)

Willem Ellis
Willem Ellis

Reputation: 5016

You'll notice I was making JSONP requests. It took me forever to find this, but the answer to this issue can be found here.

From the ticket:

JSONP requests are not guaranteed to complete (because errors are not caught). jQuery 1.5 forces the global option to false in that case so that the internal ajax request counter is guaranteed to get back to zero at one point or another.

If you want all requests to fire the events, no matter what (and at the risk of the same inconsistencies 1.4.4 exhibited), you can use the following prefilter:

jQuery.ajaxPrefilter(function( options ) {
    options.global = true;
});

Case in point: http://jsfiddle.net/X4JTx/

Upvotes: 6

Related Questions