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 7490011
In Process

The Archive Base Latest Questions

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

I have a for cycle that iterates over an associative array. foreach entry i

  • 0

I have a for cycle that iterates over an associative array. foreach entry i have to make an ajax request and fill another array with retrieved data.
The problem is that the success callback is asynchronous so it runs only after the cycle has been iterated. this means that my data array is filled with multiple copies of the last ajax request result.
This is my code (simplified):

var channels = {  
    "misured": "channel_misured",  
    "plan": "channel_plan"  
};
var data;  //Initial data is empty

function getData() {
    data = new Array();

    for (var seriesData in channels) {
        var currentData = new Array();
        $.ajax({
            type: "GET",
            url: 'http://' + serverAddress + ':' + serverPort + '/GetChannelBufferAsJsonp?channelName=' + channels[seriesData] + '&callback=?',
            dataType: "json",
            async: false,
            success: function (json) {
                var time = new Date().getTime() + 3600000;
                for (var i = 0; i < json.length; i++) {
                    currentData.push({
                        x: time + i * 30000,
                        y: json[i]
                    });
                }

                data.push({
                    id: seriesData,
                    name: "Production " + seriesData,
                    data: currentData
                });

            }, error: function () {
                console.log("error", this);
            }
        });
    }
}

So data has 2 elements (correct) but both of them comes from “plan_channel” (that is my data source).
I know this is a classic, well know “problem” (or feature) of ajax requests but i cannot see how i can get rid of it.
Can you tell how to wait for the success callback to end before going to the next iteration?

  • 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-29T15:28:28+00:00Added an answer on May 29, 2026 at 3:28 pm

    This is the only way (off the top of my head) I can see to refactor your code. Its a bit pointless though. You’ve got your set up all wrong IMO, you shouldn’t be doing it that way. The best way I can see to do it would be to send the whole array in one ajax request to the ajax script, deal with it all in the php (or whatever) and spit it back out in the format you want it in the javascript. Otherwise you’d be better off finding a better way to do this. For instance instead of having a for loop you could have an ajax request which calls back to the getData() function in the success call.

    // add a global var...
    var inLoop;
    function getData() {
        data = new Array();
    
        for (var seriesData in channels) {
            inLoop = true; // set to true
            var currentData = new Array();
    
            $.ajax({
                type: "GET",
                url: 'http://' + serverAddress + ':' + serverPort + '/GetChannelBufferAsJsonp?channelName=' + channels[seriesData] + '&callback=?',
                dataType: "json",
                async: false,
                success: function (json) {
                    // set inLoop to false to get out of loop below
                    inLoop = false;
                    var time = new Date().getTime() + 3600000;
                    for (var i = 0; i < json.length; i++) {
                        currentData.push({
                            x: time + i * 30000,
                            y: json[i]
                        });
                    }
    
                    data.push({
                        id: seriesData,
                        name: "Production " + seriesData,
                        data: currentData
                    });
    
                }, error: function () {
                    console.log("error", this);
                }
            });
    
            // loop round this bit for a while
            while(inLoop){
              // wait
            }
        }
    
    
    }
    

    This is how I would rewrite the code above:

    var channels = {
      "first" : "hello world",
      "second" : "goodbye world"
    }
    
    function getData(channel){
      $.ajax({
        url: 'http://whatever.com/channel='+channel,
        success: function(){
          getData(nextchannel);
        }
      });
    }
    

    In order to do this you might have to count the times you go through the function or something in order to be able to pass the next channel into the function again.

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

Sidebar

Related Questions

Below is a file iterarot unction I have working on that iterates over folders
I have a division operation inside a cycle that repeats many times. It so
I have three images that I want to cycle through when clicking on a
Being a Java developer in an agile development cycle, I have learnt that it
I have a table that is similar to: cycle | id | name ------|-------|------
I have this page that displays pictures in a div. I have jquery cycle
I have a jquery cycle slideshow that slides through 3 big photos and is
I have a slideshow (jquery cycle) that I use for the background of a
I have a Jquery Cycle slideshow that has a click function to animate a
I have C# code that cycles through .sql files and executes what's inside them

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.