如何使用JQuery每1秒发送一次Ajax请求? [英] How to send Ajax request on every 1s using JQuery?
本文介绍了如何使用JQuery每1秒发送一次Ajax请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用JQuery每1秒发送一次Ajax请求?
How to send Ajax request on every 1s using JQuery ?
推荐答案
您可能不希望每次都发送请求第二,正如大卫已经指出的那样。
You probably don't want to send a request every second as David already noted.
因此,使用 setInterval
是一个坏主意。
Therefore, using setInterval
is a bad idea.
相反,请考虑做以下事情:
Instead consider doing something like this:
function doAjax() {
$.ajax({
...
complete: function() {
setTimeout(doAjax,1000); //now that the request is complete, do it again in 1 second
}
...
});
}
doAjax(); // initial call will start rigth away, every subsequent call will happen 1 second after we get a response
这篇关于如何使用JQuery每1秒发送一次Ajax请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文