use*_*871 2 javascript web-services polling
我有以下代码,每隔5秒调用一次web服务.此webs服务检查列表中是否有任何内容.
setTimeout(function () {
_getListItems();
}, 5000);
Run Code Online (Sandbox Code Playgroud)
但是,我不是每隔5秒检查一次,而是想在页面加载时检查它,之后每分钟检查一次.如果列表中找到了某些内容,请每隔5秒开始调用一次.谁能帮我这个?
虽然答案已被接受,但我会发布这个,因为AJAX的组合,并setInterval允许我正在努力的应用程序取下负载均衡器并使所有公司网站脱机.
首先,让我们来讨论AJAX轮询之间setInterval和之间的差异setTimeout.
使用的优点 setInterval
使用的缺点 setInterval
使用的优点 setTimeout
使用的缺点 setTimeout
timeout + AJAX request time + response processing time,因此,一旦考虑到AJAX请求返回浏览器所花费的时间,并且浏览器需要大约5,500毫秒的间隔,则超过5,000毫秒处理响应.您可以通过跟踪AJAX请求和处理时间并从标准超时期间减去该值来解决此问题.基本上,使用的后果setTimeout是增加了代码复杂性,但是当服务器端的内容运行缓慢时,降低了自己系统的DDOS风险.
使用示例 setTimeout
function poll(url, method, period, beforeRequest, onSuccess, onError) {
var xhr = new XMLHttpRequest(),
onReadyStateChange= function() {
if (this.readyState === 4) {
if (this.status === 200 || this.status === 201) {
onSuccess(xhr);
setTimeout(sendRequest, period);
}
else if (this.status > 399) {
// Allow error handling code to retry the operation
onError(xhr, sendRequest, period);
}
}
},
sendRequest = function() {
var data = beforeRequest(xhr) || null;
xhr.open(method, url, true);
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.send(data);
};
xhr.onreadystatechange = onReadyStateChange;
setTimeout(sendRequest, period);
}
Run Code Online (Sandbox Code Playgroud)
并使用该功能:
poll("/messages", "POST", 10000,
function beforeRequest(xhr) {
return "?user_id=123";
},
function onSuccess(xhr) {
var data = JSON.parse(xhr.responseText);
// show messages...
},
function onError(xhr, sendRequest, period) {
if (xhr.status === 401) {
// show dialog to log in user
}
else {
// retry the operation
setTimeout(sendRequest, period + 10000);
}
}
);
Run Code Online (Sandbox Code Playgroud)