title: Detect when all JQuery / JavaScript / Ajax calls are finished
body: <p>I want to be able to inject a script into a page that will detect if anything is still 'going on' or not. When I say going on, I mean scripts still in execution, Ajax calls in process, or the page not being fully loaded yet. The reason being I need to check if various elements exist (via JQuery) and then do something with it. I'm trying to get this as generic as possible, as I've be injecting this in to pages with all sorts of stuff in them.</p>
  
  <p>Obviously using .ready will tell me when the page is loaded:</p>
  
  <pre><code> $( document ).ready(function() {...}); //Super easy!
  </code></pre>
  
  <p>And I can detect if an Ajax call is going on with something like:</p>
  
  <pre><code>var AjaxInProgress= function (){return jQuery.active!=0;};
  
  $( document ).ajaxStart(function() {
    console.log('Ajax stuff happening!');
  });
  $( document ).ajaxStop(function() {
    console.log('Ajax stuff finished!');
  });
  </code></pre>
  
  <p>BUT... if these Ajax calls are happening synchronously I get results like:</p>
  
  <pre><code> Ajax stuff happening!
   Ajax stuff finished!
   Ajax stuff happening!
   Ajax stuff finished!
   Ajax stuff happening!
   Ajax stuff finished!
   Ajax stuff happening!
   Ajax stuff finished!
  </code></pre>
  
  <p>So apparently there is JavaScript waiting for results from each Ajax call before firing off another one, and I get a 'false positive' after the first few because more is yet to happen.</p>
  
  <p>Is there a way I can generically detect if all scripts have executing and nothing is waiting for an Ajax response? Obviously any solution would have to avoid a race condition, and it would not cater for long polling.</p>