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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:03:51+00:00 2026-05-17T17:03:51+00:00

So, I have the following line of code, that does a for loop and

  • 0

So, I have the following line of code, that does a for loop and subjects 12 on every loop.
If I alert right before the post, it alerts the numbers as you would expect (72, 60, 48, 36, 24).
But, with the alert inside the post it alerts the number 12 five times.
Any idea why that is? Any idea where the number 12 is coming from?

function loadEstimates(option){
    for(term_length=72; term_length>=24; term_length-=12){
        var post_options = {loadEstimates: 'true', coverage_option:option, term_length:term_length};
        $.post('../includes/process.php', post_options, function(response, status){
            alert(term_length);
        });
    }
}

The second part of the question is, I want to output these in order and I’ve noticed on occasion the post will output something random like 36, 48, 24, 72, 60 because of server response times. How can I put the ‘response’ from the post into an array or something that will let me sort it before displaying it?

Thanks!!

/////// EDIT ////////
Okay, so I took some of ChessWhiz’s advice and used an array and came up with.

function loadEstimates(option){
    var term_length = new Array('72','60','48','36','24');
    var term_length_output = new Array(5);
    $.each(term_length, function(index, t_length){
        var post_options = {loadEstimates: 'true', coverage_option:option, term_length:t_length};
        $.post('../includes/process_instantquote.php', post_options, function(response, status){
            term_length_output[t_length] = response;
        });
    });

    alert(term_length_output);
}

This also solves my potential future conundrum of if I decide to use a number in the term_length that isn’t divisible by 12.
But, the term_length_output is still blank. Any idea what I am doing wrong with that?


** FINAL EDIT *****

Here is the code I ended up using. It does exactly what I need it to do. It’s probably not the most 100% correct way, but it works.

Thank you all very much for your answers and help!!!

function loadPriceEstimates(option){
    $('#package-term-options table tbody').append('<tr><td style="text-align:center;" colspan="5"><img src="images/ajax-loader-small.gif" alt="Loading..." class="small-loading"/></td></tr>').show();
    var term_length = new Array('72','60','48','36','24');
    var term_length_output = new Array();
    var count = 0;
    $.each(term_length, function(index, t_length){
        var post_options = {loadPriceEstimates: 'true', coverage_option:option, term_length:t_length};
        $.post('../includes/process_instantquote.php', post_options, function(response, status){
            if(status == 'success'){
                term_length_output[t_length] = response;
                count++;
                if(count == 5){
                    term_length_output.sort();
                    term_length_output.reverse();
                    $('#package-term-options table tbody').html('');
                    $.each(term_length_output, function(index, value){
                        $('#package-term-options table tbody').append(value);
                    });
                }
            }
        });
    });
}
  • 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-17T17:03:52+00:00Added an answer on May 17, 2026 at 5:03 pm

    Using an index variable that counts backwards by 24’s is prone to error. Instead, use a normal index, then calculate the term_length. Next, you can use an array to store the results. However, if the callback hasn’t completed running, the array won’t yet hold all the results. Lastly, you need to make a copy of the loop variable to escape the closure. Example:

    function loadEstimates(option) {
        for(i = 0; i < 5; i++) {
            var resultsIndex = i;
            var term_length = 72 - (i * 12);
            var post_options = {loadEstimates: 'true', coverage_option:option, term_length:term_length};
    
            $.post('../includes/process.php', post_options, function(response, status){
                results[resultsIndex] = response;
            });
        }
    }
    

    Then, your results will be nicely aligned in the results array. The [12, 12, 12, 12, 12] side-effect caused by closure is basically caused because the last value of term_length is getting outputted every time, which is 24 – 12 = 12, due to the way the for loop modifies it and the function closes over it.

    • 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 does the following: while(some condition) { char *line[WORDLEN]; //do
I have the following code. What it does is that it reads all key
I have the following line of code in a link on my webpage: <a
I have a unit test which contains the following line of code Site.objects.get(name=UnitTest).delete() and
In a makefile, I have the following line: helper.cpp: dtds.h Which ensures that helper.cpp
I have a project that has the following line in the additional includes section:
I have the following line of code: $('#text').append('<h2 style=color:white>afdghadfg</h2>'); If I paste this code
I have the following line: 14:48 say;0ed673079715c343281355c2a1fde843;2;laka;hello ;) I parse this by using a
I have the following file/line: pc=1 ct=1 av=112 cv=1100 cp=1700 rec=2 p=10001 g=0 a=0
I have an XML file with the following line: <VALUE DECIMAL_VALUE=0.2725 UNIT_TYPE=percent/> I would

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.