title: Handling HTTP request time out by browser in Javascript
body: <p>In my web application, I am trying to handle time out situations when calling a REST API. Here is my code which calls the API using jQuery ajax.</p>
  
  <pre><code>$.ajax({
              type: "POST",
              url: endpoint,
              data: payload,
              dataType: dataType,
              timeout: 0,
              success: successHandler,
              error: failureHandler
          });
  </code></pre>
  
  <p>The success and failure handlers are shown below.</p>
  
  <pre><code>function successHandler(data) {
      //Got the data 
  }
  
  function failureHandler(xhr, textStatus, thrownError) {
      if(xhr.status==404) {
        console.log('Page not found');
      } else if(xhr.status==408) {
        console.log('request timed out at server. You may want to retry') ;    
      } 
  }
  </code></pre>
  
  <p>If the timeout happens at the server, it is giving status 408. But sometimes, due network connectivity problems, the client (browser) itself is getting timed out because it is not able to connect to the service in specified time. I guess this is the browser behavior. What will be <code>xhr.status</code> and <code>textStatus</code> if the request gets timed out by the browser? How to handle this scenario?</p>
  
  <p>[Edit] I found some explanation in <a href="https://stackoverflow.com/questions/5225597/set-timeout-for-ajax-jquery">Set timeout for ajax (jQuery)</a>. This explains how we can set timeout in the code. But my question is that I don't want to set timeout like this. Check the code in ajax request, I set <code>timeout: 0</code> which means, there is no timeout and I am going to wait till I get the response from the server. Meanwhile, the browser may kill this request because of it's global timeout setting. I am looking for a solution which can handle this.</p>