ajax no worky
August 24, 2005 11:25 AM
Subscribe
I'm having an Ajax (Javascript) problem... it only updates once.
I wrote the following code in an effort to learn how this newfangled Ajax stuff works, but it only tells you the time once -- it refuses to update. What am I doing wrong?
Code:
var http;
function createRequest() {
var xmlhttp;
if (navigator.appName == "Microsoft Internet Explorer") {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} else {
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}
window.onload = function() {
http = createRequest();
http.onreadystatechange = writeOut;
getTime();
}
function getTime() {
http.open("get", "time?hash=" + Math.random());
http.send(null);
setTimeout("getTime()", 1000);
}
function writeOut() {
if (http.readyState == 4) {
document.getElementById('here').innerHTML = http.responseText;
}
}
posted by reklaw to technology (4 comments total)
The first thing I'd try would be moving that setTimeout into your writeOut function, inside the if block, so it only gets called after the previous request has succeeded.
posted by ook at 11:43 AM on August 24, 2005