endless loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    endless loop

    For some reason my ajax request keeps repeating. It appears to be one of these places where the code runs ahead of itself, because when I put in a breakpoint in firebug, I can step through the data. But I also see that firebug reports an error in it's execution.

    Here is my code.

    somehow line 41 never executes without a break point even though the ajax query continues returning valid results (same results) over and over again until I stop the browser.

    Code:
    var cGetTimeHttp;
    
    function getSelectWeekTime(aObj)
    {
    	if (window.ActiveXObject) 
    	{
    		cGetTimeHttp = new ActiveXObject("Microsoft.XMLHTTP");
    	}else if (window.XMLHttpRequest) {
    		cGetTimeHttp = new XMLHttpRequest();
    	}
    	var url = "/SMNO/dailyTimeReview2.exe?timeStamp=" + new Date().getTime();
    	var queryString = buildGetTime(aObj);
    	cGetTimeHttp.onreadystatechange = handleGetTimeStateChange;
    	cGetTimeHttp.open("POST", url, true);
    	cGetTimeHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    	cGetTimeHttp.send(queryString);
    }
    
    function buildGetTime(aObj)
    {
    	qString  = "";
    	qString += "&MinDate='" + aObj[0] + "'";
    	qString += "&MaxDate='" + aObj[1] + "'";
    	qString += "&Reviwer='" + aObj[2] + "'";
    	for (var i = 3; i < aObj.length; i++ )
    	{
    		qString += "&Dept"+(i-2)+"='" + aObj[i] + "'";
    	}
    
    	qString += "&method=POST";
    	return qString;
    }
    
    function handleGetTimeStateChange() 
    {
    	if (cGetTimeHttp.readyState == 4) 
    	{
    		if (cGetTimeHttp.status == 200) 
    		{
    			parseGetTimeResults();
    		}
    	}
    }
    
    function parseGetTimeResults() {
    
    	//assign the returned value to a variable
    	var returnedString = cGetTimeHttp.responseText;
    
    	//create an array to store the information
    	var aReturn = new Array();
    
    	//parse the string into the array using the semi-colon to separate each value
    	aReturn = returnedString.split('@');
    
    
    }
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    #2
    sorry about that post. I was trying to put the code in tag but hit the enter key by mistake. And did not fully explain where the error occured

    Comment

    • Claus Mygind
      Contributor
      • Mar 2008
      • 571

      #3
      the error occurs in the following section of the code. The ready state fires twice. Once with a readyState equal to 1 and the second time equal to 4. I confirmed on the console tab that the query to the server executed. In fact when I do not put in a break point, this is where the endless loop occurs. It keeps returning the data and does not appear to go to the parseGetTimeRes ults() function.

      Originally posted by Claus Mygind
      function handleGetTimeSt ateChange()
      {
      if (cGetTimeHttp.r eadyState == 4)
      {
      if (cGetTimeHttp.s tatus == 200)
      {
      parseGetTimeRes ults();
      }
      }
      }
      However when I put in the break point then it works. Here is the error message from fire bug

      Originally posted by Claus Mygind
      Firebug cannot find firebugCommandL ineAttached attribute on firebug console element, its too early for command line <div id="_firebugCon sole" style="display: none;" FirebugVersion= "1.3.2"> Window dailyTimeReview .exe

      Comment

      • Claus Mygind
        Contributor
        • Mar 2008
        • 571

        #4
        Again the problem lay outside the scope of code I included in this thread. It occurred because of a nested loops, which used the same incrementor in each loop.

        In both loops the "i" variable is used to increment. So the inner "i" reset the outer "i" which in reality is really the same variable (see below).

        That was very frustrating but at least I got to learn a little about the various state codes returned by the ajax listener
        /*
        var Http = {
        ReadyState: {
        Uninitialized: 0,
        Loading: 1,
        Loaded:2,
        Interactive:3,
        Complete: 4
        },

        Status: {
        OK: 200,

        Created: 201,
        Accepted: 202,
        NoContent: 204,

        BadRequest: 400,
        Forbidden: 403,
        NotFound: 404,
        Gone: 410,

        ServerError: 500
        },

        */


        Code:
        	for(i=0;i<obj.options.length;i++)
        	{
        		if (obj.options[i].selected )
        		{
        			if (obj.value != "")
        			{
        				var aParams = obj.value.split(';');
        				aParams[aParams.length] = document.getElementById("whoAmI").value;
        				for (var i = 0; i < aDepts.length; i++ )
        				{
        					aParams[aParams.length] = aDepts[i];
        
        				}
        				getSelectWeekTime(aParams);
        			}
        		}
        	}

        Comment

        Working...