title: ajax long polling is freezing the Server
body: <p>I have a simple JavaScript code for an AJAX request which worked just fine. For performance reasons I tried to switch to long polling, but every try freezes my server or giving me an Error 429 (too many requests). But somehow I seem to be missing a crucial point.</p>
  <p>my ajax call is</p>
  <pre><code>$(document).ready(function() {
          getData();
      });
      
  function getData() {
      
      $.ajax({ 
          type: &quot;GET&quot;,
          timeout: 60000,
          url: &quot;./ajax/fetchData.php&quot;,                           
          success: function(response){ 
              if (response != &quot;&quot;) {
                  //do someting
              } else {
                  //do something else
              }
              getData();
              /* -&gt; that was the old Version which worked fine 
              setTimeout(
                  getData,
                  15000
              );
              */
          }
      });
  };
  </code></pre>
  <p>and in my fetchData.php I have:</p>
  <pre><code>$stmt = $db-&gt;prepare(&quot;SELECT * FROM questions WHERE display = 1&quot;);
  $output = getQuestion($stmt);
  
  while($output ['question'] == NULL) {
      sleep(10);
      $output = getQuestion($stmt);
  }
  echo $output['question'];
  
  function getQuestion($stmt) {
      $stmt-&gt;execute();
      $row = $stmt-&gt;fetch(PDO::FETCH_ASSOC);
      return $row;
  }
  </code></pre>
  <p>From the example <a href="https://stackoverflow.com/questions/30252471/difference-between-a-normal-ajax-and-long-polling">here</a> that should loop until I get a record and then return this record. But somehow it seems I did not get the point.</p>