Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ajax loop question

Status
Not open for further replies.

mrcheeky

Programmer
Joined
Sep 11, 2008
Messages
6
Location
US
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-[URL unfurl="true"]www-form-urlencoded");[/URL]
	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>
 
Change your setTimeout (it has no idea what those locally scoped variables mean at the time that the method is set to run):
Code:
setTimeout("getData([!]" + [/!]data_to_send[!] + "," + [/!]zone_id[!] + "[/!])", 5000);
Cheers,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top