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

  • Home
  • SEARCH
  • 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 6932505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:42:53+00:00 2026-05-27T11:42:53+00:00

I’ve tried to put this in generic terms to make it easier to understand.

  • 0

I’ve tried to put this in generic terms to make it easier to understand. Please understand that it’s the concept, not the specific example that I’m trying to overcome.

OK, so I have a bit of JavaScript that makes a call to a server to get an object, let’s say an order. The order has properties including an array of order item IDs. I then want to loop through said IDs and get the order item records and return them in an array, like this:

function GetOrderItems(orderId){
    var orderItemIDs = GetOrderItemIDs(orderId); // array of order item IDs
    var orderItems = [];
    $.each(orderItemIDs, function(){
        var orderItem = GetOrderItem(this); // ignore this function's implementation
        orderItems.push(orderItem);
    });
    return orderItems;
}

The issue is that there could be quite a few order items, but I have to get them from the server 1 at a time (don’t ask, this won’t change). So, I want to let them know (using a jQuery UI modal dialog) how many orders there are so they have an idea of how long it will take. So, I try to inject the line noted below:

function GetOrderItems(orderId){
    var orderItemIDs = GetOrderItemIDs(orderId); // array of order item IDs
    var orderItems = [];
    $("#modalDiv").dialog({title: orderItemIDs.length + " order items to get."});
    $.each(orderItemIDs, function(){
        var orderItem = GetOrderItem(this); // ignore this function's implementation
        orderItems.push(orderItem);
    });
    return orderItems;
}

The problem is that dialog shows, but only when everything is all complete. I learned from a previous question that you could use setTimeout to get the modal to show, but how do I return the array that I’m building?

function GetOrderItems(orderId){
    var orderItemIDs = GetOrderItemIDs(orderId); // array of order item IDs
    var orderItems = [];
    $("#modalDiv").dialog({title: orderItemIDs.length + " order items to get."});
    setTimeout(function(){
        $.each(orderItemIDs, function(){
            var orderItem = GetOrderItem(this); //ignore this function's implementation
            orderItems.push(orderItem);
        });
        return orderItems;
    },0);
}

I’ve also doing a second setTimeout function to show progress, but it goes straight to the last one and none of the functions run, like this:

function GetOrderItems(orderId){
    var orderItemIDs = GetOrderItemIDs(orderId); // array of order item IDs
    var orderItems = [];
    $("#modalDiv").dialog({title: orderItemIDs.length + " order items to get."});
    setTimeout(function(){
        $.each(orderItemIDs, function(){
            setTimeout(function(){
                var orderItem = GetOrderItem(this); //ignore this function's implementation
                orderItems.push(orderItem);
            },500);
        });
        return orderItems;
    },0);
}

Thanks in advance!

  • 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-27T11:42:53+00:00Added an answer on May 27, 2026 at 11:42 am

    You’ll have to do it asynchronously. There’s no other way to get the UI to refresh. See this related question. That means your calling code will have to change from:

    var orderItems = GetOrderItems(orderId);
    // do something with orderItems
    

    To:

    GetOrderItems(orderId, function (orderItems) {
        // do something with orderItems
    })
    

    Presumably, you’ve only been able to get away with this synchronous code by making your server requests synchronous, thus tying up the browser, etc. If you are going to take the plunge into asynchronocity, you might as well take advantage of all the potential benefits. For example, your modal could be updated to indicate how many orders remain to be downloaded:

    function GetOrderItems(orderId, callback){
        var orderItemIDs = GetOrderItemIDs(orderId); // array of order item IDs
        var orderItems = [];
        var orderItemCount = orderItemIDs.length;
        $("#modalDiv").dialog({title: "<span id=\"orderItemRemaining\">" + orderItemCount + "</span> order items to get."});
        $.each(orderItemIDs, function(){
            $.get("/getOrderItem?orderId=" + this, function (orderItem) {
                orderItems.push(orderItem);
                var remaining = orderItemCount - orderItems.length;
                $("#orderItemRemaining").text(remaining);
                if (!remaining) {
                    callback(orderItems);
                }
            });
        });
    }  
    

    Edit: To limit concurrent requests, don’t use $.each(). Instead start just the number of concurrent requests that you want, and then initiate the rest of the requests in the request’s success handler:

    function GetOrderItems(orderId, callback){
        var orderItemIDs = GetOrderItemIDs(orderId); // array of order item IDs
        var orderItems = [];
        var orderItemCount = orderItemIDs.length;
        $("#modalDiv").dialog({title: "<span id=\"orderItemRemaining\">" + orderItemCount + "</span> order items to get."});
        var maxConcurrent = 3;
        for (var i = 0; i < maxConcurrent; i++) {
            getOrderItem(orderItemIDs.shift());
        }
        function getOrderItem(orderItemID) {
            $.get("/getOrderItem?orderId=" + orderItemID, function (orderItem) {
                orderItems.push(orderItem);
                var remaining = orderItemCount - orderItems.length;
                $("#orderItemRemaining").text(remaining);
                if (remaining) {
                    getOrderItem(orderItemsIDs.shift());
                }
                else
                    callback(orderItems);
                }
            });
        }
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,

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.