106

I was trying to find some more information on the following jQuery function:

jQuery.active

It is described to test the number of active connections to a server and will evaluate true when the number of connections is zero.

I could not find any information about this function on the jQuery site and was wondering if anyone knew where I could.

3
  • Is it now or was it ever a function?
    – danronmoon
    Aug 12, 2021 at 1:15
  • This takes me back lol
    – RyanP13
    Aug 13, 2021 at 10:45
  • 1
    This question seems to be the canonical resource so it'd be nice to get some clarification and editorial updates for this decade about it since it's still available in 3.6.0. I don't think it was ever a function.
    – danronmoon
    Aug 13, 2021 at 13:46

2 Answers 2

167

This is a variable jQuery uses internally, but had no reason to hide, so it's there to use. Just a heads up, it becomes jquery.ajax.active next release. There's no documentation because it's exposed but not in the official API, lots of things are like this actually, like jQuery.cache (where all of jQuery.data() goes).

I'm guessing here by actual usage in the library, it seems to be there exclusively to support $.ajaxStart() and $.ajaxStop() (which I'll explain further), but they only care if it's 0 or not when a request starts or stops. But, since there's no reason to hide it, it's exposed to you can see the actual number of simultaneous AJAX requests currently going on.


When jQuery starts an AJAX request, this happens:

if ( s.global && ! jQuery.active++ ) {
  jQuery.event.trigger( "ajaxStart" );
}

This is what causes the $.ajaxStart() event to fire, the number of connections just went from 0 to 1 (jQuery.active++ isn't 0 after this one, and !0 == true), this means the first of the current simultaneous requests started. The same thing happens at the other end. When an AJAX request stops (because of a beforeSend abort via return false or an ajax call complete function runs):

if ( s.global && ! --jQuery.active ) {
  jQuery.event.trigger( "ajaxStop" );
}

This is what causes the $.ajaxStop() event to fire, the number of requests went down to 0, meaning the last simultaneous AJAX call finished. The other global AJAX handlers fire in there along the way as well.

6
  • 2
    I saw it described in a book jQuery Novice to Ninja (2010)
    – jmav
    Jul 25, 2011 at 9:27
  • @Nick : As per your explanation of .ajaxStop will be executed after every ajax call completed. But, it will be called if all ajax calls were completed. Can you please correct me if I am wrong. Jun 22, 2013 at 16:49
  • 28
    This is now called $.active, btw.
    – Ryan Bigg
    Jul 5, 2013 at 6:20
  • 4
    @RyanBigg isn't $ simply an alias for jQuery, so both are still true?
    – arxpoetica
    Dec 21, 2016 at 17:30
  • 2
    @ArxPoetica only if you're running jQuery without noConflict.
    – Ryan Bigg
    Dec 28, 2016 at 21:31
19

For anyone trying to use jQuery.active with JSONP requests (like I was) you'll need enable it with this:

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

Keep in mind that you'll need a timeout on your JSONP request to catch failures.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.