Ajax loop question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mrcheeky
    New Member
    • Sep 2008
    • 7

    Ajax loop question

    Hi,

    Can anyone find why this setTimeout() loop isn't working - I'm getting:

    'data_to_send' is undefined

    Any help much appreciated, thanks.

    BTW, ajax.js contains my handling for creating the XMLHttpRequest, called createRequest() .

    Code:
    <html>
    <head>
    
    <script src="ajax.js" type="text/javascript"></script>
    
    <script language="JavaScript">
    
    function getData(data_to_send,zone_id)
    { 
    	var url = "realtime_read_values.php";
    	var req = create_request(); 
    	req.open("POST", url, true); 
    	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    	req.send(data_to_send); 
    	req.onreadystatechange = function()
    	{
    		if(req.readyState == 4 && req.status == 200)
    		{
    			document.getElementById(zone_id).innerHTML  = req.responseText;
    			//setTimeout("getData(window.data_to_send)", 3000); // this doesn't work
    		} 
    	}
    	setTimeout("getData(data_to_send,zone_id)", 5000);
    } 
    
    </script>
    	
    <body onload="getData('graph_request=graph_counter1','zone1'); getData('graph_request=graph_counter2','zone2'); getData('graph_request=graph_counter3','zone3');">
    
    <div id="zone1">
    waiting for data..
    </div> 	
    
    <div id="zone2">
    waiting for data..
    </div> 	
    
    <div id="zone3">
    waiting for data..
    </div> 	
    
    </body>
    </html>
  • Ferris
    New Member
    • Oct 2007
    • 101

    #2
    Hi
    I think , as a string type of setTimeout's 1st parameter, your parameters (data_to_send,z one_id) will not pass to the function getData.

    Code:
    setTimeout("getData(data_to_send,zone_id)", 5000);
    you can use anonymous function to do it.
    try this:

    Code:
    setTimeout(function(){ getData(data_to_send,zone_id); }, 5000);
    Regards.

    Comment

    Working...