阿贾克斯心跳使用的setInterval,preventing多个呼叫在同一时间多个、时间、在同一、阿贾克斯

2023-09-10 16:14:41 作者:说好的不分离

我希望做一个AJAX调用使用jQuery在后台心跳,让我的Web应用程序可以检查是否有新的数据。例如每10秒

I wish to make an AJAX call using jQuery as a "heartbeat" in the background so that my web application can check for new data. For example every 10 seconds.

我看到的其他职位,它可以使用的setInterval()来调用由它来完成呼叫的毫秒每隔X数量的方法。

I have seen on other posts that it is possible to use setInterval() to call a method which does the call every X number of milliseconds.

不过,会发生什么?我还是希望它完成了最后一个请求,我不希​​望再呼叫被鼓动的时候已经有之一的进展。这可能导致同样的信息被放置在至页两次!

However, what happens if my AJAX call takes more than 10 seconds? I still want it to complete the last request and I don't want another call to be instigated when there is already one in progress. This could result in the same information being placed on to the page twice!

这是实施本我的方法来等待原始AJAX调用的方式来完成,而不是简单的每10秒,当原来的可能尚未完成。

What is the way to implement this for my method to wait for the original AJAX call to be completed, rather than simply "every 10 seconds", when the original one may not already be complete.

推荐答案

尝试从阿贾克斯成功的功能设置另一个超时:

try setting another timeout from your ajax success function:

function poll() {
    setTimeout( function() {
        $.ajax(.......).success( function(data) {
            poll();  //call your function again after successfully calling the first time.
        });
    }, 10000);
}