2013-03-14 59 views
3

我在使用jQuery AJAX请求here创建长轮询的指令。 下面是我的代码:在turbolink中使用jQuery在Rails中进行长轮询

: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冲突?

谢谢。

回答

3

为什么民意调查再次,因为你是在完成回调再次调用函数POLL

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

也不要混淆timeoutsetTimeout,这里超时意味着如果AJAX调用不会在8秒内归还将触发错误回电

LIVE DEMO

+0

是的,这就是问题所在。感谢您指出了这一点。 – AdamNYC 2013-03-14 04:27:10