chrismassey
Programmer
Hey,
I am using a javascript/AJAX HTTP Request to automatically refresh a perl script which simply generates a random number. The plan is to reload the perl script every 5 seconds therefore displaying a new random number each time. However, a javascript error is produced, although the perl script runs and is printed, it never refreshes.
Im guessing that there is something wrong with the fRefresh function, and any help or suggestions as to what I am doing wrong will be much appreciated...
Thanks,
Chris
I am using a javascript/AJAX HTTP Request to automatically refresh a perl script which simply generates a random number. The plan is to reload the perl script every 5 seconds therefore displaying a new random number each time. However, a javascript error is produced, although the perl script runs and is printed, it never refreshes.
Im guessing that there is something wrong with the fRefresh function, and any help or suggestions as to what I am doing wrong will be much appreciated...
Code:
<html>
<head>
<script type="text/javascript">
window.onload = fInitial;
var xhr = null;
function fInitial() {
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
else {
if (window.ActiveXObject) {
try {
xhr = new ActiveXObject("Microsoft.XMLHttp");
}
catch (e) {}
}
}
if (xhr) {
fRefresh();
}
else {
alert("XML Http Request Could Not Be Processed.");
}
}
function fRefresh() {
var url="rng.pl";
xhr.open("GET",url,false);
xhr.onreadystatechange = fDisplay;
xhr.send(null);
setTimeout("fRefresh",5 * 1000);
}
function fDisplay() {
document.getElementById("DisplayNum").innerHTML=xhr.responseText;
}
</script>
</head>
<body>
<span id="DisplayNum"></span>
</body>
</html>
Thanks,
Chris