By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,692 Members | 1,553 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,692 IT Pros & Developers. It's quick & easy.

endless loop

Claus Mygind
100+
P: 571
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. var cGetTimeHttp;
  3.  
  4. function getSelectWeekTime(aObj)
  5. {
  6.     if (window.ActiveXObject) 
  7.     {
  8.         cGetTimeHttp = new ActiveXObject("Microsoft.XMLHTTP");
  9.     }else if (window.XMLHttpRequest) {
  10.         cGetTimeHttp = new XMLHttpRequest();
  11.     }
  12.     var url = "/SMNO/dailyTimeReview2.exe?timeStamp=" + new Date().getTime();
  13.     var queryString = buildGetTime(aObj);
  14.     cGetTimeHttp.onreadystatechange = handleGetTimeStateChange;
  15.     cGetTimeHttp.open("POST", url, true);
  16.     cGetTimeHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  17.     cGetTimeHttp.send(queryString);
  18. }
  19.  
  20. function buildGetTime(aObj)
  21. {
  22.     qString  = "";
  23.     qString += "&MinDate='" + aObj[0] + "'";
  24.     qString += "&MaxDate='" + aObj[1] + "'";
  25.     qString += "&Reviwer='" + aObj[2] + "'";
  26.     for (var i = 3; i < aObj.length; i++ )
  27.     {
  28.         qString += "&Dept"+(i-2)+"='" + aObj[i] + "'";
  29.     }
  30.  
  31.     qString += "&method=POST";
  32.     return qString;
  33. }
  34.  
  35. function handleGetTimeStateChange() 
  36. {
  37.     if (cGetTimeHttp.readyState == 4) 
  38.     {
  39.         if (cGetTimeHttp.status == 200) 
  40.         {
  41.             parseGetTimeResults();
  42.         }
  43.     }
  44. }
  45.  
  46. function parseGetTimeResults() {
  47.  
  48.     //assign the returned value to a variable
  49.     var returnedString = cGetTimeHttp.responseText;
  50.  
  51.     //create an array to store the information
  52.     var aReturn = new Array();
  53.  
  54.     //parse the string into the array using the semi-colon to separate each value
  55.     aReturn = returnedString.split('@');
  56.  
  57.  
  58. }
  59.  
Feb 9 '09 #1
Share this Question
Share on Google+
3 Replies


Claus Mygind
100+
P: 571
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
Feb 9 '09 #2

Claus Mygind
100+
P: 571
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 parseGetTimeResults() function.

@Claus Mygind
However when I put in the break point then it works. Here is the error message from fire bug

@Claus Mygind
Feb 9 '09 #3

Claus Mygind
100+
P: 571
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
},

*/


Expand|Select|Wrap|Line Numbers
  1.     for(i=0;i<obj.options.length;i++)
  2.     {
  3.         if (obj.options[i].selected )
  4.         {
  5.             if (obj.value != "")
  6.             {
  7.                 var aParams = obj.value.split(';');
  8.                 aParams[aParams.length] = document.getElementById("whoAmI").value;
  9.                 for (var i = 0; i < aDepts.length; i++ )
  10.                 {
  11.                     aParams[aParams.length] = aDepts[i];
  12.  
  13.                 }
  14.                 getSelectWeekTime(aParams);
  15.             }
  16.         }
  17.     }
  18.  
Feb 9 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.