+2  A: 

I think that's because the for loop body is not a scope for the callback function closure, the scope is the container function.

Try this:

function loadEstimates(option){
    for(term_length=72; term_length>=24; term_length-=12){
        postProcess(term_length);
    }
}

function postProcess(term_length) {
        var post_options = {loadEstimates: 'true', coverage_option:option, term_length:term_length};
        $.post('../includes/process.php', post_options, function(response, status){
            alert(term_length);
        });
}

For more info see:

Nicolas Bottarini
Thanks for the information! I decided that the for loop in general was the wrong way to go with this in case there were future modifications that deemed the math worthless. I edited m original post above to show my solution to the for loop. But, my issue still remains with the array not working. Any thoughts on that?
LostInQuery
Well, now you have another problem. The assignment to the array is inside an asynchronous callback and the alert of the array is in the calling function. In this case the alert comes first and then when server answers and you get the ajax responses, the array assignment happens. My advice is to make an unique ajax call that processes all the information at once.
Nicolas Bottarini
+1  A: 

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.

ChessWhiz
I was just thinking, that if in the future, I decided to go with a number that was not calculable by 12 then I was going to have issues. So I did kind of like you sugested and put the numbers into an array. So, the term_length is good now, but still have issues with the results being in an array.
LostInQuery
Edited my post above with an extension of the issue using your thoughts.
LostInQuery
See my edit to avoid closure on the array index.
ChessWhiz
The reason that the code in your edit doesn't alert data for term_length_output is that the alert() runs before the callbacks have returned, so the array isn't populated by the time it's being displayed to you. Try adding a delay before the alert.
ChessWhiz
Your code gives me the same issue mine gives me. 'undefined' or nothing at all when I alert the array.
LostInQuery
Again, the callback function in $.post() needs time for the server to respond. If you call alert() immediately, the array won't yet be populated. I believe that's the issue here.
ChessWhiz
+1  A: 

The problem has to do with the way for works: it uses the same variable throughout its lifespan (rather than instantiating new ones), meaning that same variable is being bound to your $.post() callback every time. AJAX calls are asynchronous, and that callback isn't being called until after your loop has finished and term_length has made its way down to 12.

For a discussion of the general problem outside of JavaScript, see "Python's lambda is broken!"

The solution, then, is to, somehow or another, get JavaScript to instantiate a new variable every iteration so it's not changed out from under you during the looping. One way to do that is to pass it through a function. See Nicolas Bottarini's answer for the code.

Joey Adams