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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:01:41+00:00 2026-05-17T18:01:41+00:00

OK, this problem is very odd. I am receiving a list of items using

  • 0

OK, this problem is very odd. I am receiving a list of items using getJSON(). For each of the returned items, I perform a lookup using getJSON() again. Upon return of the second lookup, I try to append a variable from the scope of the for-loop to the DOM but the output is the SAME. The weird thing is that when I issue an alert() of the variable right before the second getJSON(), the variables change like it should.

Is this a bug? It seems like getJSON is caching something…

$.getJSON(
"http://someurl", 
    function(data) {
        var items = data.items;

        for(i=0; i<items.length; i++) {
            var i = items[i];
            //when doing an alert of 'i' here, it changes...
            $.getJSON(
                "http://anotherurl",
                    function(r) {
                        $("#feed").append(i.some_property); 
                        //appended item should be different upon each loop
                        //but ends up appending the same thing every time!!
                    }
            )
        }
    }
)
  • 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-17T18:01:41+00:00Added an answer on May 17, 2026 at 6:01 pm

    Ajax is Asynchronous.

    You probably manage to loop all the way through the outside loop before the first HTTP request made by the inner getJSON comes back, so i is on the last value before the inner callback is run for the first time.

    When you add an alert, the loop execution is paused until you click the OK button and this gives the HTTP request time to respond.

    You need to create a new i (or other variable) in a different scope.

    for(var i=0; i<items.length; i++) {
         // IMPORTANT: Don't forget to use var. 
         // DO NOT use globals without a very good reason
         // Oh, i is a var but you use the keyword on the *second* time you use it
         // Don't do that, it's confusing!
         do_the_inside_thing(items[i]);
    }
    
    function do_the_inside_thing(foo) {
        // A new function is a new scope
            $.getJSON(
                "http://anotherurl",
                    function(r) {
                        $("#feed").append(foo.some_property); 
                    }
            );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first question on S.O. I have a very odd problem. Below
Very odd problem as this is working perfectly on our old Classic ASP site.
I've been experiencing this very odd problem lately in Heroku. I develop and test
I'm seeing a very odd problem in a WPF list box where the MouseEnter
This seems to be a very odd problem that I cannot figure out for
I'm using Django's message framework, and I have a very odd problem where my
This problem is very strange and I'm hoping someone can help me. For the
The very act of asking this question suggests that my approach to this problem
Problem : This is a probably a very basic question, but how do I
I'm stuck in this very easy problem (I hope it is for you). I

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.