title: JQuery Ajax Long Polling - " poll function won't get called again until both the ajax call is complete and (at-least) thirty (30) seconds have passed"
body: <p>I'm looking in to doing long polling for a "notifications" system on a website. I've never done this before and thus I'm looking for information online about it. I am looking here: <a href="http://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery" rel="nofollow">http://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery</a></p>
  
  <p>In the section about long polling it says:
  "Timeout: Set a timeout (in milliseconds) for the request. The timeout period starts at the point the $.ajax call is made. Here, we set the timeout to 30 seconds. This means our poll function won't get called again until both the ajax call is complete and (at-least) thirty (30) seconds have passed."</p>
  
  <p>Saying poll won't be called until BOTH the ajax call is complete AND at least 30 seconds have passed.</p>
  
  <p>When I add this in to Javascript though, poll is called over and over again in just the time it takes for the ajax call to finish. It doesn't wait 30 seconds.</p>
  
  <pre><code>(function poll() {
  
           $.ajax({ url: "/myapp/messages/messages/checkMessages", success:
           function(data){alert("YO");}, complete: poll, timeout: 30000 });
  
      })(); 
  </code></pre>
  
  <p>Am I doing something wrong, or is this how long polling is suppose to work?</p>
  
  <p>Also, is this efficient? Would this scale with site traffic?</p>
  
  <p>Thanks for the help!</p>
  
  <pre><code>(function poll(){
             setTimeout(function(){
                $.ajax({ url: "/myapp/messages/messages/checkMessages", success: function(data){
                    console.log("Polling!");
                }, complete: poll, timeout: 30000});
            }, 30000);
          })();
  </code></pre>