Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6998407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:24:28+00:00 2026-05-27T20:24:28+00:00

I have some code that looks like this (I left out the parts that

  • 0

I have some code that looks like this (I left out the parts that are unimportant for the question):

$.ajax({  
    type: "POST",  
    url:  "prepXML.php",  
    data: "method=getartists&user=" + userName + "&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&user=" + userName + "&artist=" + artistNamePOST + "&playcount=" + artistPlaycount,  
        dataType: "xml",
        success: function(xml){
            // calculate and output some stuff
        });                     
    }
});  

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).

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.

To better understand what I need this for and what the app does, here is a link to the app. 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..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:24:29+00:00Added an answer on May 27, 2026 at 8:24 pm

    I’d consider only retrieving track info for artists that a user has clicked on. Or possibly retrieving all of the data via a single request (perhaps then after it is retrieved process it in batches via setTimeout()).

    But something along the lines of the following might work to do only five requests at a time:

    $.ajax({  
        type: "POST",  
        url:  "prepXML.php",  
        data: "method=getartists&user=" + userName + "&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 < 5 && i < 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&user=" + userName + "&artist=" + artistNamePOST + "&playcount=" + artistPlaycount,  
            dataType: "xml",
            success: function(xml){
                // calculate and output some stuff
            },
            complete : function() {
                if (callback) callback();
            });                     
        }
    });
    

    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 .each() 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 nextBatch() (which is local to the success handler and thus has access to the local variables) will run a loop that calls getTracks() only 5 times, but starting processing from where we left off the previous time. Meanwhile getTracks() has been updated slightly to accept a callback function so when its 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 nextBatch() again.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that looks like this: someFunc(value) { switch(value){ case 1: case
In VS 2005, I have some code that looks like this: ifs.open(foo); while (!ifs.eof())
I have some XML code that looks like this <SEARCHRESULTS> <FUNCTION name=BarGraph> <PARAMETER name=numList></PARAMETER>
Ok, I have some C# code that looks like this and I was wondering
I have some Objective-C code that looks like this: #define myVar 10 float f
Let's say I have some code (using CherryPy) that looks like this: import cherrypy
I have some code that looks like: static const std::string and( AND ); This
I have some code in Netbeans 6.1 editor that looks like this fooString(8) fooString(8)
I have html code that looks roughly like this: <div id=id1> <div id=id2> <p>some
We have some html that looks like this: <form id=MyUserControl runat=server method=post> <script language=javascript

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.