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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:02:37+00:00 2026-06-13T12:02:37+00:00

for (var i = 0; i < 5; i++) { with (x = new

  • 0
for (var i = 0; i < 5; i++) {
    with (x = new XMLHttpRequest()) open("GET","d.php?id=" + i), send(null), onreadystatechange = function() {
       if (x.readyState == 4 && x.status == 200) alert(i);
    }
}

Now I want each time when readyState = 4, it should alert correct value of i for which URL was called. Currently, it alert only for once and output alert is 5

  • 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-13T12:02:39+00:00Added an answer on June 13, 2026 at 12:02 pm

    If you want to use with to retain i, you’d either need to add it to an object that also references the xhr object:

    for(var i=0;i<5;i++){
        with({i:i, xhr:new XMLHttpRequest()}) {
            xhr.open("GET","d.php?id=" + i);
            xhr.send(null);
            xhr.onreadystatechange=function(){
                if (xhr.readyState == 4 && xhr.status == 200)
                    alert(i);
            }
        }
    } 
    

    Or you’d need to create the xhr outside the with and add i to it.

    var xhr;
    for(var i=0;i<5;i++){ 
        (xhr = new XMLHttpRequest()).i = i;
        with(xhr) {
            open("GET","d.php?id=" + i);
            send(null);
            onreadystatechange=function(){
                if (readyState == 4 && status == 200)
                    alert(i);
            }
        }
    } 
    

    But if you want a proper, future-proof solution, make the handler in a variable scope that provides the variables needed for the handler.

    function doRequest(i, xhr) {
        xhr.open("GET","d.php?id=" + i);
        xhr.send(null);
        xhr.onreadystatechange=function(){
            if (xhr.readyState == 4 && xhr.status == 200)
                alert(i);
        }
    }
    

    And call it like this:

    for(var i=0;i<5;i++){
        doRequest(i, new XMLHttpRequest());
    } 
    

    Or if you insist upon inlining the function as some do, you could do it like this:

    for(var i=0;i<5;i++){
        (function (i, xhr) {
            xhr.open("GET","d.php?id=" + i);
            xhr.send(null);
            xhr.onreadystatechange=function(){
                if (xhr.readyState == 4 && xhr.status == 200)
                    alert(i);
            }
        }(i, new XMLHttpRequest());
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

currently i using this code in phonegap application var xmlhttp = new XMLHttpRequest(); xmlhttp.open(GET,http://192.168.1.19:8080/searchMobile?categoryRequest=true,
Why xhr.responseXML.getElementsByClassName('clazz') doesn't work? Here is the js: var xhr = new XMLHttpRequest(); xhr.onreadystatechange
I have the next code: js: $('[id^=check-]').change(function(){ var new_id = this.id.replace(/check/, 'new'), real_id =
var ref1 = new Firebase(http://gamma.firebase.com/myuser/123,456); ref1.set(123,456); var on1 = ref1.on(value, function(snapshot) { console.log(snapshot.val()); });
var qx = require('qooxdoo'); var t= new T(4080); var t= new qx.T(4080); // none
This question shows the usage of **var = new Object(); and that's something totally
I have this anonymous type : var t= new {a=1,b=lalala,c=DateTime.Now}; How can I make
Can you clarify this please? public static void MyMethod() { var context= new MyModel.Entities();
can i use custom attributes with linkbutton? like this: var linkb= new LinkButton(); linkb.ID
public string Process<T>(string name, string address) { var dataObj= new Data<T>(); List<T> currentList= dataObj.GetAll(name);

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.