Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I a group jQuery AJAX queries?
    text
    copied!<p>I have some code that looks like this (I left out the parts that are unimportant for the question):</p> <pre><code>$.ajax({ type: "POST", url: "prepXML.php", data: "method=getartists&amp;user=" + userName + "&amp;amount=" + amount, dataType: "xml", success: function(xml) { $("artist", xml).each(function(){ // calculate and output some stuff and then call getTracks() getTracks(artistName, artistNamePOST, artistPlaycount, divId, artistId); }); } }); function getTracks(artistName, artistNamePost, artistPlaycount, divId, artistId){ $.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 }); } }); </code></pre> <p>When this is run, it calls getTracks() fifty times and makes quite some (server) CPU load in very short time until it all gets done. What I would like to do is to group AJAX getTrack() queries by for example 5 at a time, wait until those five are done, then call the next five, wait until the next five are done, call the next five etc. The purpose of this would be to make fewer queries at basically the same time (less and more evenly spread CPU load). </p> <p>I am unsure how or even if this can be done since it kind of partially beats the point of AJAX, but I would still like to make this work if possible. Could someone please point me into the right direction? Thank you.</p> <p>To better understand what I need this for and what the app does, here is a link to the <a href="http://lastfm.cmikavac.net/" rel="nofollow"><strong>app</strong></a>. it can be tried out with any lastfm nick (if you don't have one, you can use mine - "pootzko"). I hope I am allowed to put the link in the post(?), if not, feel free to remove it..</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