【问题标题】:ajax polling: requests sent more frequently than setTimeoutajax 轮询:请求发送的频率高于 setTimeout
【发布时间】:2014-04-18 18:29:08
【问题描述】:

我有以下 ajax 轮询代码:

function pollStatus(url){
    $.get(url, function(response){
        if (response.uploaded === null){
            setTimeout(pollStatus(url), 5000);
        }
    };
}

当我查看网络选项卡时,我看到请求几乎每秒发送一次,而不是 settimeout 中指定的 5 秒。我在这里做错了什么?

【问题讨论】:

    标签: javascript jquery ajax polling


    【解决方案1】:

    你的函数被立即调用,因为你必须在timeout中传递一个参数,所以使用匿名函数:

     setTimeout(function() {
         pollStatus(url)
     }, 5000);
    

    【讨论】:

      猜你喜欢
      • 2011-04-20
      • 1970-01-01
      • 2019-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多