Forum Moderators: open

Message Too Old, No Replies

problem with setInterval and IE

         

Lolalola

9:07 pm on Jul 8, 2009 (gmt 0)



Hi,
with Mozila everything work, but with IE didn't work. Page didn't reload.
What's wrong?


<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>

Gibble

9:46 pm on Jul 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Shouldn't the syntax be without quotes?

setInterval(sendRequest, 1000*2);

Lolalola

10:10 pm on Jul 8, 2009 (gmt 0)



No, without quotes show error.

And this is sample with quotes:
[w3schools.com...]

daveVk

11:00 pm on Jul 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Rather than relying on timing try

window.onload = sendRequest;

in place of

setInterval("sendRequest()", 1000*2);()", 1000*2);

Lolalola

12:58 pm on Jul 9, 2009 (gmt 0)



Like this:
window.onload = function() {
int = setInterval("sendRequest()", 2000);
};

?
No, didn't work

Fotiman

1:35 pm on Jul 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What version of IE are you using? I just tried your original post in IE8 and it ran fine.

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.

Fotiman

1:46 pm on Jul 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Note, it's possible that IE is fetching a cached version of the file. Try using a cache killing technique to force IE to fetch a new version of select.php.


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.

Lolalola

7:04 pm on Jul 9, 2009 (gmt 0)



Fotiman>
thanks a lot ;)