Simple XHR long poll without jQuery

I am trying to read a simple long polling system (yes, I do not want to use any ready-made script as I want to extract from it). I use a node server, where I can easily transfer / write my data back to the client without calling result.end();. How do I make this client side? I just want this to be a simple and not very good reserve for users using ie <= 9, as the best browsers get fast and easy to use websocket.

Long question: how to make a long poll in plain JS without jQuery or another structure? (Or is there a better way than a lengthy survey).

+3
source share
1 answer

, ?

  var xhr = new XMLHttpRequest();
  xhr.responseType = 'blob';//or 'text', 'json', ect. there are other types. 
  xhr.timeout = 60000;//milliseconds until timeout fires. (1 minute)
  xhr.onload = function(e2){
    var blob = xhr.response;
    //handle response data
  }
  xhr.ontimeout = function(){
    //if you get this you probably should try to make the connection again.
    //the browser should've killed the connection. 
  }
  xhr.open('GET', "/path/to/URL.cmd?param1=val1&param2=val2", true);
  xhr.send();

, timeout , , spec responseType . , .

+1

All Articles