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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:45:38+00:00 2026-06-13T23:45:38+00:00

function updateItem(str) { var stocks =new Array(GOOG,MSFT,AAP,JDAS,G); for(var i=0; i<stocks.length;i++) { xmlHttp=GetXmlHttpObject(); if (xmlHttp==null)

  • 0
function updateItem(str)
{
    var stocks =new Array("GOOG","MSFT","AAP","JDAS","G");
    for(var i=0; i<stocks.length;i++)
    {

        xmlHttp=GetXmlHttpObject();    
        if (xmlHttp==null)
        {
            alert ("Browser does not support HTTP Request");
            return;
        }

        var url="php/updateCart.php";
        url=url+"?q="+stocks[i];
        url=url+"&sid="+Math.random();

        xmlHttp.onreadystatechange= function(){
            if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
            {
                document.getElementById(stocks[i])
                .innerHTML=xmlHttp.responseText;
            }
        } 

        xmlHttp.open("GET",url,true);
        xmlHttp.send(null); 

    }
}    

function startOff()
{

    if(interval != -1)
    {
        clearInterval(interval);
    }

    interval = setInterval(function() {updateItem()}, 1000);
} 

I’m trying to update multiple divisions in a form in a interval the problem is the for loop is going to fast for the request to finish. I didn’t realize this til I made a bunch of alerts and notice when I push enter slowly…I know you can do it maybe with a setTimeout but the syntax I just can’t seem to get down right. Since the I don’t have a name to the function to check the readystate can someone give me a hint on the syntax. Or if there a more simple way of doing it…I would appreciate it.

  • 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-13T23:45:39+00:00Added an answer on June 13, 2026 at 11:45 pm

    Edit: I got things a bit mixed up and thought javascript had block scope. That’s what happens when you try to solve problem in that many languages I suppose. The problem is still sort of the same, but the solution isn’t. I’m still not going to solve this using the timeout you used (or starting the next request when the previous one is done), as I don’t think that’s the correct way to go. Instead, I am going to solve the underlying problem which the timeout technique only tries to work its way around.

    The problem isn’t so much in that the for loop is “too fast”. The problem is that the variables you use in your callback function have a scope that is larger than the for loop body. This means that you set the same variables to different values each time. As such, the variables will have the final value when the callback function is finally called.

    One way to fix this would be to move the creation of the request to a new function like this:

    function updateAllItems()
    {
        var stocks =new Array("GOOG","MSFT","AAP","JDAS","G");
        for(var i=0; i<stocks.length;i++)
        {
            updateItem(stocks[i]);
        }
    }
    
    function updateItem(stock)
    {
        var xmlHttp=GetXmlHttpObject(); // Do note, we need the var keyword here to
                                        // make sure this variables uses this scope
                                        // rather than global scope
        if (xmlHttp==null)
        {
            alert ("Browser does not support HTTP Request");
            return;
        }
    
        var url="php/updateCart.php";
        url=url+"?q="+ stock;
        url=url+"&sid="+Math.random();
    
        xmlHttp.onreadystatechange= function(){
            if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
            {
                document.getElementById(stock)
                .innerHTML=xmlHttp.responseText;
            }
        }
    
        xmlHttp.open("GET",url,true);
        xmlHttp.send(null);
    }
    

    That is probably the easier way to understand, but personally I like the way below more. It basically does the same thing, but using a little Javascript trickery with an anonymous function we call immediately, we no longer need a separate function like that, but can instead handle this inline:

    function updateAllItems()
    {
        var stocks =new Array("GOOG","MSFT","AAP","JDAS","G");
        for(var i=0; i<stocks.length;i++)
        {
    
            (function ()
            {
                var xmlHttp=GetXmlHttpObject(); // Do note, we need the var keyword here to
                                                // make sure this variables uses this scope
                                                // rather than global scope
                var i2 = i;
    
                if (xmlHttp==null)
                {
                    alert ("Browser does not support HTTP Request");
                    return;
                }
    
                var url="php/updateCart.php";
                url=url+"?q="+ stocks[i];
                url=url+"&sid="+Math.random();
    
                xmlHttp.onreadystatechange= function(){
                    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
                    {
                        document.getElementById(stocks[i])
                             .innerHTML=xmlHttp.responseText;
                    }
                }
            })();
    
            xmlHttp.open("GET",url,true);
            xmlHttp.send(null);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

function getSelectedCopyDates() { var arr = new Array(); //for every row that has a
function getWindowsUserName() { var WinNetwork = new ActiveXObject(WScript.Network); var urlToSite = http://localhost/index.php?nph-psf=0&HOSTID=AD&ALIAS= + WinNetwork.UserName;
function getWindowsUserName() { var WinNetwork = new ActiveXObject(WScript.Network); var urlToSite = createCustomURL(WinNetwork.UserName); document.getElementById(psyncLink).src =
function countChars(elm) { if (elm.nodeType == 3) { // TEXT_NODE return elm.nodeValue.length; } var
function Apple(){ this.name=apple; } function Orange(){ this.name=orange; this.apple = new Apple(); this.apple.onCalled=function(){ alert(this.name); }
function checkuser(user) { var ret = false; $.ajax({ type:'POST', url:'user.php', async:false, data:{'user':user}, success:function (data)
function killsession() { // global $_SESSION; $_SESSION = array(); if (session_id() != || isset($_COOKIE[session_name()]))
function getDbValue() { alert($('[data-bind]').length); alert($('[data-bind][0].data-bind')); alert($('[data-bind][0].value')); jQuery.each($('[data-bind]'), function(databind,key) { alert(key); alert(databind); alert(databind[key].data-bind); }) }
function array_searchRecursive( $needle, $haystack, $strict=false, $path=array() ) { if( !is_array($haystack) ) { return false;
I have a loop kind of like this var lastUpdateTime = (new Date()).getTime(), diffTime

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.