每十秒钟轮询一次 [英] Polling every 10 seonds
问题描述
在下面的代码中,我期望这将创建一个轮询循环,该循环将每10秒触发一次,以从服务器获取作业状态报告".似乎每秒创建100个以上的呼叫.
In the code below, I had the expectation that this would create a polling loop that would fire off every 10 seconds to get a "job status report" from the server. It seems it is creating over 100 calls per second.
我从这个出色的 ="a
I got my education from this excellent article, but I seem to be misreading it.
function UpdateEstimates() {
var request = { jobID: ExportVM.get("ticketID") };
var jqxhr = $.ajax({
type: "GET",
url: AppGlobals.serverURL + "GetJobStatus",
data: request,
contentType: "application/json; charset=utf-8",
complete: UpdateEstimates,
timeout: 10000,
dataType: "json"
});
jqxhr.done(function (data) { NewEstimates(data); });
jqxhr.fail(function (data) { alert('new estimates request failed'); });
}
该呼叫的开销非常低,因此我可以轻松处理1秒钟的更新,但是我真的不太喜欢连续100/sec的速率.有什么方法可以在呼叫循环中引入延迟吗?
The call is very low overhead, so I can handle easily 1 second updates, but I really don't like the continuous 100/sec rate very much. Is there some way to introduce a delay into the call loop?
推荐答案
超时没有按照您的实际想法进行.它只是为该特定呼叫设置超时(最大允许时间). 您必须将其包装在setTimeout函数中.
timeout in $.ajax is not doing what you actually think. It just sets the timeout for that particular call (maximum allowed time). You will have to wrap this in setTimeout function.
这篇关于每十秒钟轮询一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!