javascript setInterval:调用重叠吗? [英] javascript setInterval: do calls overlap?
问题描述
假设我有 setInterval(PostToServer,1000);
。 PostToServer
函数发出ajax帖子,这可能需要一秒钟以上。那么接下来会发生什么:在第一次呼叫未完成或等待呼叫结束之前进行第二次呼叫再进行新呼叫?
Suppose I have setInterval(PostToServer, 1000);
. PostToServer
function makes ajax post, which may take longer than a second. So what happens then: a second call is made while the first hasn't finished or the end of call is awaited before making new one?
推荐答案
呼叫重叠。
setInterval
确保定期运行功能,无需等待之前的结果。
setInterval
ensures that the functions are run regularly, without waiting for the previous result.
如果要等待响应,请将间隔方法更改为轮询器。当时间过去且服务器已经响应时,请再次请求。
If you want to await the response, change the interval method to a poller. When the time has passed AND the server has already responded, request again.
由于服务器响应在响应后不会发生太大变化,您还可以添加AJAX方法的回调函数中的 setTimeout
处理程序。
Since the server response won't change too much right after the response, you can also add a setTimeout
handler in the callback function of your AJAX method.
这篇关于javascript setInterval:调用重叠吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!