如何使用setTimeout打破AJAX轮询 [英] How to break out of AJAX polling done using setTimeout
本文介绍了如何使用setTimeout打破AJAX轮询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想实现此答案中提到的AJAX轮询。现在我想在服务器返回特定数据值时中断轮询。怎么做?
I want to implement AJAX polling mentioned in this answer. Now I want to break out of polling when server return particular data value. How to do that?
推荐答案
尝试这样的事情(你改变条件,将continuePolling设置为你需要的任何东西):
Try something like this (where you change the condition to set continuePolling false to whatever you need):
(function poll() {
var continuePolling = true;
setTimeout(function() {
$.ajax({
url: "/server/api/function",
type: "GET",
success: function(data) {
console.log("polling");
if (data.length == 0)
{
continuePolling = false;
}
},
dataType: "json",
complete: function() { if (continuePolling) { poll(); }),
timeout: 2000
})
}, 5000);
})();
这篇关于如何使用setTimeout打破AJAX轮询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文