AdamNYC
AdamNYC

Reputation: 20425

Long polling using jQuery in Rails with turbolink

I am following an instruction to create long polling using jQuery AJAX request here. Below is my code:

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

But instead of timeout for 8 seconds, this code polls continuously. Am I doing something wrong, or does this somehow conflict with turbolink gem that I am using with Rails 3.2?

Thank you.

Upvotes: 3

Views: 1314

Answers (1)

Dakait
Dakait

Reputation: 2620

why it polls again because you are calling the function poll again in the complete callback

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

also dont confuse the timeout with setTimeout, here timeout means that if the ajax call does not return within 8 seconds it will trigger the error call back

LIVE DEMO

Upvotes: 2

Related Questions