0

hi after searching in the net about how to use the long polling in JavaScript I ended up with three ways, they are mentioned here briefly,but they are implemented using JQuery. I am confused which one to use in case that the AJAX request that I send to the server is asynchronous GET request ,and I don't know how many time it could take.

here is an example AJAX request:

function asynchGETRequest(method,url){ var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { console.log("ok"); } }; xhttp.open(method, url, true); xhttp.send(); return (xhttp.responseText); } var clientFunctions={ getAnswers : function(callback){ var res=asynchGETRequest("GET", "http://localhost:9000/answers"); callback(JSON.stringify(res)); } } clientFunctions.getAnswers (function(){ //do some code here after the ajax request is ended }); 

can some one guide me please?

1 Answer 1

1

I think I found the solution here

function loadFile(sUrl, timeout, callback){ var args = arguments.slice(3); var xhr = new XMLHttpRequest(); xhr.ontimeout = function () { console.error("The request for " + url + " timed out."); }; xhr.onload = function() { if (xhr.readyState === 4) { if (xhr.status === 200) { callback.apply(xhr, args); } else { console.error(xhr.statusText); } } }; xhr.open("GET", url, true); xhr.timeout = timeout; xhr.send(null); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.