Asynchronous jQuery request timing issue
<p>As a learning exercise I've hacked together a script for an SO <a href="http://meta.stackoverflow.com/questions/13847/display-statistics-have-subject-badges-for-accepted-answers/16062#16062">feature request</a> (for the purposes of this question please ignore the merits or otherwise of that request). In the script I've encountered a technical issue that my limited javascript knowledge can't get past and I'd appreciate suggestions on how to resolve it.</p>

<p>To avoid spamming the server I use some search hacks to determine the number of answers and accepted answers for a tag. This involves using window.setTimeout() to callback to a function that sends a get request for each tag, increasing the timeout on each call to stagger the requests.</p>

<p>To get the results in a single request involves appending &amp;pagesize=1 to the end of the url in the get request, so that the number of pages in the results gives you the total number of results without having to make any further requests. </p>

<p>A side affect of this approach is that subsequent page views use &amp;pagesize=1 and I only see a single entry. I attempt to resolve this by firing another query with &amp;pagesize=30 to reset it afterwards, but as it is all asynchronous the timing of the last query can result in the pagesize either being 1 or 30, depending on which request completes first. I've tried adding a further timeout and callback for this "reset" query but it hasn't really helped.</p>

<p>Is there a means to monitor the queries, waiting until all have been completed, then once they have all completed send the reset request? Or is there another approach that I could take?</p>
