Using XMLHttpRequest in a loop

M

marss

I faced to a strange problem I couldn't solve myself.
I use XMLHttpRequest in order to periodically ask server for some
data. I use code I have used many times before (but not in a loop).
It works fine in Firefox but not in IE :(.
In IE it executes only time, then "send" method executes as it should
but method attached to "onreadystatechange" never called.
I mean
.... send ...
.... an event handler for onreadystatechange ...
.... send ...
.... send ...
.... send ...
.... send ...

Could anybody give me a suggestion about reasons of that behaviour?

Regards, Mykola
http://marss.co.ua

Below is a snippet to illustrate the situation.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>String Build Performance Tester</title>
</head>

<body>
<script type="text/javascript">
function InitializeXMLHttpRequest(req)
{
if (req != null)
req.abort();

if (typeof XMLHttpRequest != "undefined")
req = new XMLHttpRequest();
else
{
try
{
req=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try { req=new ActiveXObject("Microsoft.XMLHTTP"); }
catch(oc){ req=null;}
}
}
return req;
}

var reqChat = null;
reqChat = InitializeXMLHttpRequest(reqChat);
reqChat.onreadystatechange = ProcessCheckStatusResult;

function ProcessCheckStatusResult(){
status+='+';
if (reqChat.readyState == 4){
if (reqChat.status == 200){
alert("ok");
}
}
}

function CheckStatus(){
status+=' send';
var url = "2.htm?t=" + new Date().getTime();
reqChat.open('GET', url, true);
reqChat.send(null);
}
status='';

setInterval(CheckStatus, 5000);

</script>
</body>
</html>
 
G

GArlington

I faced to a strange problem I couldn't solve myself.
I use XMLHttpRequest in order to periodically ask server for some
data. I use code I have used many times before (but not in a loop).
It works fine in Firefox but not in IE :(.
In IE it executes only time, then "send" method executes as it should
but method attached to "onreadystatechange" never called.
I mean
... send ...
... an event handler for onreadystatechange ...
... send ...
... send ...
... send ...
... send ...

Could anybody give me a suggestion about reasons of that behaviour?

Regards, Mykolahttp://marss.co.ua

Below is a snippet to illustrate the situation.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>String Build Performance Tester</title>
</head>

<body>
<script type="text/javascript">
function InitializeXMLHttpRequest(req)
{
if (req != null)
req.abort();

if (typeof XMLHttpRequest != "undefined")
req = new XMLHttpRequest();
else
{
try
{
req=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try { req=new ActiveXObject("Microsoft.XMLHTTP"); }
catch(oc){ req=null;}
}
}
return req;

}

var reqChat = null;
reqChat = InitializeXMLHttpRequest(reqChat);
reqChat.onreadystatechange = ProcessCheckStatusResult;

function ProcessCheckStatusResult(){
status+='+';
if (reqChat.readyState == 4){
if (reqChat.status == 200){
alert("ok");
}
}

}

function CheckStatus(){
status+=' send';
var url = "2.htm?t=" + new Date().getTime();
reqChat.open('GET', url, true);
reqChat.send(null);}

status='';

setInterval(CheckStatus, 5000);

</script>
</body>
</html>

Try to re-Init your reqChat in CheckStatus()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top