如何使用JQuery每秒发送一次Ajax请求?
正如David已经指出的那样,您可能不希望每秒发送一次请求。
因此,使用setInterval是一个不好的主意。
相反,考虑像这样做:
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