title: Asynchronous JavaScript polling freezes the browser using setTimeout or setInterval
body: <p>I write a polling script to receive newly created data records. I want to execute the call in every <code>N</code> seconds.</p>
  
  <p>I tried <code>setTimeout()</code> and <code>setInterval()</code> to run the polling <strong>asynchronously</strong>, but both freeze the browser while executing the <code>Polling()</code> function, which is really strange for me.</p>
  
  <p>I call the <code>StarPolling()</code> function when the page is loaded. <code>APICall()</code> function is a <code>jQuery $.POST</code> function which is working well - and async - in any other situations.</p>
  
  <p>This is the code I use with <code>setTimeout()</code></p>
  
  <pre><code>var pollinginterval = 5000;
  function StartPolling()
  {
      setTimeout(Polling, pollinginterval);
  }
  
  function Polling()
  {
      [... some code ...]
  
      var api_call = 'API_URL';
      var api_call_parameters = {
          [...]
      };
      APICall(api_call, api_call_parameters, function(json_response)
      {
          /* this is the callback belongs to the $.POST request */
  
          [... some code ...]
  
          setTimeout(Polling, pollinginterval);
      });
  }
  </code></pre>
  
  <p>The version I tried using <code>setInterval()</code> is very similar except the recursive call.</p>
  
  <p>I can not use Workers or HTML5 sockets for this because cross-browser support is a must.</p>
  
  <p>Is there any way to run the polling in a <strong>REAL asynchronous way</strong>, or using a <strong>new 'thread'</strong> with JavaScript without freezing the browser?</p>
  
  <p><strong>UPDATE:</strong>
  This is how the <code>APICall()</code> operates:</p>
  
  <pre><code>function APICall(call, parameters, success_callback)
  {
      $.post(apibase + "" + call,parameters)
      .done(function(response){
          try
          {
              var json_response = $.parseJSON(response);
          }
          catch(error)
          {
              [...]
          }
  
          if(json_response.header.status == "OK")
          {
              success_callback(json_response);
          }
          else if(json_response.header.status == "error")
          {
              [...]
          }
      })
      .fail(function(error) {
          [...]
      });
  }
  </code></pre>
  
  <p><strong>UPDATE</strong>: I am testing the polling with a normal and private browser (firefox) window at the same time to try the functionality. I just noticed, that the problem only occurs when both windows are running the polling simultaneously.</p>
  
  <p>Maybe it is a firefox bug...</p>