【发布时间】:2017-08-31 07:55:08
【问题描述】:
嗨,在网上搜索了如何使用JavaScript中的long polling后,我最终得出了三种方法,它们被简单地提到了here,但它们是使用JQuery实现的。如果我发送到服务器的 AJAX 请求是异步 GET 请求,我不知道该使用哪一个,而且我不知道需要多少时间。
这是一个 AJAX 请求示例:
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
});
有人可以指导我吗?
【问题讨论】:
标签: javascript ajax long-polling