Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'd consider only retrieving track info for artists that a user has clicked on. Or possibly retrieving <em>all</em> of the data via a single request (perhaps then after it is retrieved process it in batches via <code>setTimeout()</code>).</p> <p>But something along the lines of the following might work to do only five requests at a time:</p> <pre><code>$.ajax({ type: "POST", url: "prepXML.php", data: "method=getartists&amp;user=" + userName + "&amp;amount=" + amount, dataType: "xml", success: function(xml) { var artists = $("artist", xml), i = 0, c = 0; function getTracksComplete() { if (--c === 0) nextBatch(); } function nextBatch() { for(; c &lt; 5 &amp;&amp; i &lt; artists.length; i++, c++) { // artists[i] is the current artist record // calculate and output some stuff and then call getTracks() getTracks(artistName, artistNamePOST, artistPlaycount, divId, artistId, getTracksComplete); } } // Optional - if you need to calculate statistics on all the artists // then do that here before starting the batches of getTracks calls artists.each(function() { /* do something */ }); // Kick of the first batch of 5 nextBatch(); } }); function getTracks(artistName, artistNamePost, artistPlaycount, divId, artistId, callback){ $.ajax({ type: "POST", url: "prepXML.php", data: "method=gettracks&amp;user=" + userName + "&amp;artist=" + artistNamePOST + "&amp;playcount=" + artistPlaycount, dataType: "xml", success: function(xml){ // calculate and output some stuff }, complete : function() { if (callback) callback(); }); } }); </code></pre> <p>The above was just off the top of my head (so I didn't have time to build it to scale or to paint it), but the idea is that instead of using <code>.each()</code> to loop through all of the artists at once we will cache the jQuery object and do a few at a time. Calling the function <code>nextBatch()</code> (which is local to the success handler and thus has access to the local variables) will run a loop that calls <code>getTracks()</code> only 5 times, but starting processing from where we left off the previous time. Meanwhile <code>getTracks()</code> has been updated slightly to accept a callback function so when <em>its</em> ajax call completes (and note we do this on complete rather than success in case of errors) it can let the main process know that it has finished. Within the callback we keep track of how many have completed and when they all have call <code>nextBatch()</code> again.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload