title: Show waiting page while server reboots
body: <p>I have a server and create a web interface for it, if a user presses the reboot button on the page, then the user is redirected to <code>reboot.php</code> where he is supposed to see a spinner gif till the server is reachable again and the server is getting rebooted via shell execute. If the server is reachable then i need to redirect to <code>main.php</code></p>
  
  <p>So i created the following function. The function starts with a timeout of 5 seconds, because otherwise it would instantly load <code>main.php</code> because the reboot command takes it's time.</p>
  
  <p><strong>reboot.php</strong></p>
  
  <pre><code>    $ret = false;
  
      test();
  
      function test()
      {
          setTimeout
          (
              function()
              {
                   $ret = ping("www.google.de");
                   if ($ret === false)
                   {
                       test();
                   }
                   else
                   {
                       window.location.href = "main.php";
                   }
              },
              3000
          );
      }
  
      function ping(url)
      {
          $.ajax
          (
              {
                  url: url,
                  success: function(result)
                  {
                      alert('reply');
                      return true;
                  },     
                  error: function(result)
                  {
                      alert('timeout/error');
                      return false;
                  }
              }
          );
      }
  </code></pre>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;
  
      &lt;div class="col-lg-6 col-lg-offset-3" id="mydiv"&gt;
          &lt;div class="mydiv_inhalt"&gt;
              &lt;h2&gt;Rebooting...&lt;/h2&gt;
              &lt;p&gt;This can take about 2 minutes.&lt;/p&gt;
              &lt;center&gt;
                  &lt;div class="waiting" id="waiting" style="margin-top:30px; margin-left: 0px;"&gt;
                      &lt;center&gt;&lt;img class="loading_table" src="http://www.securenet.com/sites/default/files/spinner.gif" alt="spinner gif"&gt;&lt;/center&gt;
                  &lt;/div&gt;
              &lt;/center&gt;
          &lt;/div&gt;
      &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>ajax.php</strong></p>
  
  <pre><code>$cmd        = filter_input(INPUT_POST, "cmd");
  if ( isset( $cmd ) &amp;&amp; $cmd == "check_online_status" )
  {
      echo "true";
  }
  </code></pre>
  
  <p>In my <code>ajax.php</code> i simply return "true" if the call was successful.
  However, my logic does not work, it seems that my code only tries to make one call to my <code>ajax.php</code> and then never tries again and i get <code>net::ERR_CONNECTION_REFUSED</code> in the console. </p>
  
  <p>I put many alerts into my code, but they are not executed, so i guess it does not try to call <code>ajax.php</code> a second time after getting <code>net::ERR_CONNECTION_REFUSED</code>.</p>
  
  <p>My only idea would be to wait enough time and then redirect to <code>main.php</code>, but this is not a good solution because luck is needed, what if the time was not enough etc.</p>