www.webdeveloper.com
Results 1 to 3 of 3

Thread: AJAX for loop

  1. #1
    Join Date
    Apr 2011
    Posts
    3

    AJAX for loop

    Code:
    	/** Update time **/
    			function updateTime(){
    				var names = document.getElementsByName(\"time_elapsed\");
    				var total = names.length;
    				for(var i=0; i<total; i++){
    					var currentName = names.item(i);
    					var currentId = currentName.getAttribute(\"id\");
    					loadXMLDoc(\"GET\",\"update.php?id=\"+ currentId,function(){
    						if(xmlhttp.readyState==4 && xmlhttp.status==200){
    							currentName.innerHTML= xmlhttp.responseText;
    						}
    					});
    				
    				}
    			}
    I wrote this code myself so I understand I'm probably missing something simple. But I've been error testing on it all day with no luck. For some reason the looped variables work fine outside loadXMLDoc(). But inside it only displays the last i value.

  2. #2
    Join Date
    Apr 2011
    Posts
    3

    Thanks for the help *crickets*

    I found out that you have to use a synchronous XMLHTTPRequest. In this general XMLHTTPRequest function below if you set trueFalse to "false" (without the quotes) it will be a synchronous request.

    ie: loadXMLDoc(\"GET\",false,\"example.php\", function(){ //code here });

    Synchronous simply means that the code will halt until that specific request is finished and has returned a value. The problem I was having lied in the fact that the for loop itterated through faster then the XMLHTTPRequest could keep up, because it was not halting and waiting for a reply. The for loop pushed the last value to the function and therefore that is the only value that was changed.


    Code:
    /** General XMLHTTP Request **/
    				var xmlhttp;
    				function loadXMLDoc(GETPOST,trueFalse,url,cfunc){
    					if (window.XMLHttpRequest)
    					  {// code for IE7+, Firefox, Chrome, Opera, Safari
    					  xmlhttp=new XMLHttpRequest();
    					  }
    					else
    					  {// code for IE6, IE5
    					  xmlhttp=new ActiveXObject(\"Microsoft.XMLHTTP\");
    					  }
    					xmlhttp.onreadystatechange=cfunc;
    					xmlhttp.open(GETPOST,url,trueFalse);
    					xmlhttp.send();
    				}

  3. #3
    Join Date
    Apr 2011
    Posts
    3

    Revised loop

    I also revised the for loop and request\action into different function with the for loop calling on the request\action function with every iteration.

    Code:
    function updateTime(){
    				var total = document.getElementsByName(\"time_elapsed\").length;
    				for(var i=0; i<total; i++){
    					pleaseGod(i);
    				}
    			}
    			
    			function pleaseGod(i){
    				var currentName = document.getElementsByName(\"time_elapsed\").item(i);
    				var currentId = currentName.getAttribute(\"id\");
    				loadXMLDoc(\"GET\",false,\"update.php?id=\"+ currentId,function(){
    					if(xmlhttp.readyState==4 && xmlhttp.status==200){
    						currentName.innerHTML= xmlhttp.responseText;
    					}
    				});
    			}

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles