【问题标题】:Long polling using jQuery in Rails with turbolink在带有 turbolink 的 Rails 中使用 jQuery 进行长轮询
【发布时间】:2013-03-02 07:37:14
【问题描述】:

我正在按照说明使用 jQuery AJAX 请求 here 创建长轮询。 以下是我的代码:

:javascript
  (function poll(){
      $.ajax({ url: $("comment").data("url"), success: function(data){
          alert(data.comment);
      }, dataType: "json", complete: poll, timeout: 8000 });
  })();

但这段代码不是超时 8 秒,而是连续轮询。我做错了什么,或者这是否与我在 Rails 3.2 中使用的 turbolink gem 冲突?

谢谢。

【问题讨论】:

    标签: jquery ruby-on-rails polling turbolinks


    【解决方案1】:

    为什么会再次轮询,因为您在完成回调中再次调用函数 poll

        (function poll(){
              $.ajax({ url: $("comment").data("url"), success: function(data){
                  alert(data.comment);
              }, dataType: "json", complete: poll, timeout: 8000 });
    -----------------------------------------^ //here
          })();
    

    也不要混淆timeoutsetTimeout,这里的timeout表示如果ajax调用在8秒内没有返回就会触发错误回调

    LIVE DEMO

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-15
      • 2018-11-16
      • 1970-01-01
      相关资源
      最近更新 更多