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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:25:43+00:00 2026-05-20T08:25:43+00:00

I use the ajax-request queue as posted here: Wait until all jQuery Ajax requests

  • 0

I use the ajax-request queue as posted here: Wait until all jQuery Ajax requests are done?

Now i wrote the following code to implement it:

for (var i=0; i<3; i++) {
    $.ajaxQueue({
        url: '/action/',
        data: {action:'previous',item:i*-1},
        type: 'GET',
        success: function(data) {
            $('.item-history .itemlist').prepend(data['item_add']);
            $('.item-history .itemlist:first .index').html(data['newitemindex']);
            //alert(data['newitemindex'])       
        };
    });

As long as i use the alert to proof the response from the server, everything works fine. But as soon as i run the code, as shown, without the alert, data[‘newitemindex’] behaves as it was a global variable – it always returns the value of the last item.

I tried to set up a jsfiddle on this, but as i have never used that site, i could not get the ajax to work. If somebody wants to have a look at it anyway: http://jsfiddle.net/marue/UfH5M/26/

  • 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-20T08:25:44+00:00Added an answer on May 20, 2026 at 8:25 am

    Your code is setting up three ajax calls, and then applying the result of each of them to the same elements (there’s no difference in the selectors you use inside your success function). For the $('.item-history .itemlist') elements, you should see the result of each call prepended to the elements because you’re using prepend(), but for the $('.item-history .itemlist:first .index') elements, you’re using html() which replaces the elements’ contents, and so for those you’ll see the result of the last call that completes.


    Off-topic: To fix that, you’re probably going to want to use your loop variable in some way in the success function. That could lead you to a common mistake, so here’s an example of the mistake and how to avoid it.

    Let’s say I have these divs:

    <div id='div1'></div>
    <div id='div2'></div>
    <div id='div3'></div>
    

    And I want to use three ajax calls to populate them when I click a button, using a loop counter from 1 to 3. I might think I could do it like this:

    $('#btnGo').click(function() {
      var i;
    
      for (i = 1; i <= 3; ++i) {
        $.ajax({
          url: "/agiba4/" + i,
          dataType: "text",
          success: function(data) {
            // THIS NEXT LINE IS WRONG
            $('#div' + i).html(data);
          },
          error: function(xhr, status, err) {
            $("<p/>").html(
              "Error, status = " + status + ", err = " + err
              ).appendTo(document.body);
          }
        });
      }
    });
    

    Live example (which fails)

    But (as indicated) that doesn’t work. It doesn’t work because each success function we create has an enduring reference to the i variable, not a copy of its value as of when the success function was created. And so all three success functions see the same value for i, which is the value when the function is run — long after the loop is complete. And so (in this example), they all see the value 4 (the value of i after the loop finishes). This is how closures work (see: Closures are not complicated).

    To fix this, you set it up so the success function closes over something that isn’t going to be updated by the loop. The easiest way is to pass the loop counter into another function as an argument, and then have the success function close over that argument instead, since the argument is a copy of the loop counter and won’t be updated:

    $('#btnGo').click(function() {
      var i;
    
      for (i = 1; i <= 3; ++i) {
        doRequest(i);
      }
    
      function doRequest(index) {
        $.ajax({
          url: "/agiba4/" + index,
          dataType: "text",
          success: function(data) {
            $('#div' + index).html(data);
          },
          error: function(xhr, status, err) {
            $("<p/>").html(
              "Error, status = " + status + ", err = " + err
              ).appendTo(document.body);
          }
        });
      }
    });
    

    Live example

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a web app in PHP which makes use of Ajax requests
I don't want to use jQuery, but I'd like to use Ajax to do
I am trying to use jQuery's ajax functionality to update data from a web
Is it possible to use a jQuery ajax call to perform Forms Authentication with
I'm using jQuery in our Java project and 3 AJAX requests to refresh 3
I use the $.post function to submit an ajax request as such: $('.typeAform').submit(function(){ $.post('/home',
I have this site here I have an ajax request that calls a php
I'm building a Django site and trying to use the request.is_ajax() function... But it's
I don't currently use ajax.net though I would be open to it if it
Does excess use of AJAX affects performance? In context of big size web-applications, how

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.