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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:22:12+00:00 2026-06-09T16:22:12+00:00

I am adding deferred getJSON calls to an array inside a for loop that

  • 0

I am adding deferred getJSON calls to an array inside a for loop that reference local variables inside their success function. The problem I am having is that when the success function is called, the local variable is taking the value from the last iteration of the loop. See below example:

var calls = [];
var arr = ['a','b','c'];
for (var a in arr) {
    calls.push(
        $.getJSON(window.location, function() { alert(arr[a]); })
    );
}
$.when.apply($,calls);

jsFiddle: http://jsfiddle.net/Me5rV/

This results in three alerts with the value ‘c’, whereas I would like the values ‘a’, ‘b’, and ‘c’. Is this possible?

EDIT: The below works, but I’m not entirely sure why this differs?

var calls = [];
var arr = ['a','b','c'];
for (var a in arr) {
    calls.push(
        $.getJSON(window.location, function(x) {
            alert(x);
       }(arr[a]))
    );
}
$.when.apply($,calls);

jsFiddle: http://jsfiddle.net/Me5rV/1/

  • 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-06-09T16:22:14+00:00Added an answer on June 9, 2026 at 4:22 pm

    Review what a loop like this:

    var a = [];
    
    for( var i = 0; i < 3; ++i ) {
        a.push( function() {
            alert(i);
        });
    }
    

    actually does:

    var a = [], i = 0;
    
    a.push( function(){
        alert(i);
    });
    
    i++;
    
    a.push( function() {
        alert(i);
    });
    
    i++;
    
    a.push( function() {
        alert(i);
    });
    
    i++;
    
    //condition isn't met, loop terminates
    
    alert(i) //alerts 3 because i is 3 now.
             //That's why all the functions alert 3, because they all 
             //refer to this i and its value is 3
    

    Now you could do this instead (repetition removed):

    a.push( function(i){
        return function() {
             alert(i); //Refers to the i passed as argument to the outer function
                       //not the global one
                       //The local i has whatever value the global i had when it was passed
                       //as argument
        };
    }(i));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Adding methods to native JavaScript objects like Object, Function, Array, String, etc considered as
Adding member variables to a class certainly boosts the memory footprint of its instances
Adding support for Unicode passwords it an important feature that should not be ignored
I understand that if you have some function in a linq query that does
When adding a remote (or closing) from another local git repository, is there a
When the twisted reactor is running and an exception occurs within a deferred that
Will adding an argument to member function gonna change the behavior of the function?
Adding HTML/any tags to either side of selection - Javascript The problem: After creating
adding arrays inside dictionaries, inside more arrays. I asked a question and Jakebird451 helped
Adding long text inside an HTML element such as a DIV, the element wraps

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.