Comet (lengthy survey) and XmlHttpRequest status

I will play a little with raw XmlHttpRequestObjects + Comet Long Polling. (Normally, I would allow GWT or another framework for this, but I want to know more about that.)

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 call longPoll()in an if statement, which checks for status > 0, because I was faced with the fact that when I leave the page (looking somewhere else or reloading it), the last unnecessary cometary call is sent, [And in Firefox it even calls serious problems while reloading the page, for some reason I don’t quite understand yet.]

Question: Is this statuschecking the correct way to solve this problem or is there a better solution?

+5
2

- , false - , .

+3

.... , script , , . -, , . , bloch

0

All Articles