Comet (long polling) and XmlHttpRequest status
Posted
by chris_l
on Stack Overflow
See other posts from Stack Overflow
or by chris_l
Published on 2010-04-12T19:05:06Z
Indexed on
2010/06/08
7:22 UTC
Read the original article
Hit count: 285
I'm playing around a little bit with raw XmlHttpRequestObjects + Comet Long Polling. (Usually, I'd let GWT or another framework handle of this for me, but I want to learn more about it.)
I wrote the following code:
function longPoll() {
var xhr = createXHR(); // Creates an XmlHttpRequestObject
xhr.open('GET', 'LongPollServlet', true);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
...
}
if (xhr.status > 0) {
longPoll();
}
}
}
xhr.send(null);
}
...
<body onload="javascript:longPoll()">...
I wrapped the longPoll()
call in an if statement that checks for status > 0
, because I encountered, that when I leave the page (by browsing somewhere else, or by reloading it), one last unnecessary comet call is sent. [And on Firefox, it even causes severe problems when doing a page reload, for some reason I don't fully understand yet.]
Question: Is that status
check the correct way to handle this problem, or is there a better solution?
© Stack Overflow or respective owner