Forum Moderators: open
<html>
<head>
<script type="text/javascript">
setInterval("sendRequest()", 1000*2);
function createRequestObject() {
var req;
if(window.XMLHttpRequest){
// IE 7, Firefox, Safari, Opera...
req = new XMLHttpRequest();
} else if(window.ActiveXObject) {
// Internet Explorer 6, 5
req = new ActiveXObject("Microsoft.XMLHTTP");
} else {
alert('Problem creating the XMLHttpRequest object');
}
return req;
} var http = createRequestObject();
function sendRequest() {
http.open('GET', 'select.php', true);
http.onreadystatechange = handleResponse;
http.send(null);
}
function handleResponse() {
if(http.readyState == 4 && http.status == 200){
var response = http.responseText;
if(response)
{
document.getElementById('text').innerHTML = response;
}
}
}
</script>
</head>
<body>
<div id="text"></div>
</body>
</html>
And this is sample with quotes:
[w3schools.com...]
?
No, didn't work
Note, it is better to use setTimeout and setInterval with a function reference instead of a quoted string.
setInterval(sendRequest, 1000*2);
will result in the same thing as:
setInterval("sendRequest()", 1000*2);
But will better expose scope issues and does not require that the JavaScript interpreter evaluate the string as a function. In general, my advice would be to avoid using the string version.
http.open('GET', 'select.php?ck=' + (new Date()).getTime(), true);
In this example, I'm appending a querystring with the current time so that each time the request will be unique.