title: Repeat JavaScript AJAX request until status change
body: <p>I am using AJAX to make and retrieve an API call from PHP file and then using jQuery IF statements to display certain data.</p>
  
  <p>The data returned come with a few different statuses, E.g.: "Ready", "In progress", "DNS" and "Error". Each status has a message that comes with it.</p>
  
  <p>For one particular status, "In progress", I want to keep cycling through the JavaScript function until the status changes to something else.  If the status is "In progress", it can take up to 1 minute for it to change, so if possible, I would like to cycle through the JavaScript function in 5 second intervals.</p>
  
  <p>I have included my jQuery below.</p>
  
  <pre><code>    $(document).ready(function() {
  
      //Stops the submit request
      $("#myAjaxRequestForm").submit(function(e){
             e.preventDefault();
      });
  
      //checks for the button click event
      $("#myButton").click(function(e){
             $("#ajaxResponse").empty();
              //get the form data and then serialize that
              dataString = $("#myAjaxRequestForm").serialize();
  
              //get the form data using another method
              var hostName = $("input#hostName").val();
              dataString = "host=" + hostName;
  
              //make the AJAX request, dataType is set to json
              //meaning we are expecting JSON data in response from the server
              $.ajax({
                  type: "GET",
          url: "api.php",
                  data: dataString,
                  dataType: "json",
  
                  //if received a response from the server
                  success: function( response ){
                  var data = response.status;
                      if ((data == 'READY' &amp;&amp; response.endpoints[0].statusMessage == 'Ready' )) {
                      $("#ajaxResponse").append("&lt;b&gt;Status:&lt;/b&gt; " + response.endpoints[0].statusMessage+ "&lt;br&gt;");
                      $("#ajaxResponse").append("&lt;b&gt;Host:&lt;/b&gt; " + response.host+ "&lt;br&gt;");
                      $("#ajaxResponse").append("&lt;b&gt;Port:&lt;/b&gt; " + response.port+ "&lt;br&gt;");
                      $("#ajaxResponse").append("&lt;h3&gt;Endpoint [0]&lt;/h3&gt;&lt;b&gt;Server Name:&lt;/b&gt; " + response.endpoints[0].serverName+ "&lt;br&gt;");
                      $("#ajaxResponse").append("&lt;b&gt;IP Address:&lt;/b&gt; " + response.endpoints[0].ipAddress+ "&lt;br&gt;");
                      $("#ajaxResponse").append("&lt;b&gt;Grade:&lt;/b&gt; " + response.endpoints[0].grade+ "&lt;br&gt;");
                      $("#ajaxResponse").append("&lt;h3&gt;Endpoint [1]&lt;/h3&gt;&lt;b&gt;Server Name:&lt;/b&gt; " + response.endpoints[1].serverName+ "&lt;br&gt;");
                      $("#ajaxResponse").append("&lt;b&gt;IP Address:&lt;/b&gt; " + response.endpoints[1].ipAddress+ "&lt;br&gt;");
                      $("#ajaxResponse").append("&lt;b&gt;Grade:&lt;/b&gt; " + response.endpoints[1].grade+ "&lt;br&gt;");
                      $("#ajaxResponse").append("&lt;h3&gt;Endpoint [2]&lt;/h3&gt;&lt;b&gt;Server Name:&lt;/b&gt; " + response.endpoints[2].serverName+ "&lt;br&gt;");
                      $("#ajaxResponse").append("&lt;b&gt;IP Address:&lt;/b&gt; " + response.endpoints[2].ipAddress+ "&lt;br&gt;");
                      $("#ajaxResponse").append("&lt;b&gt;Grade:&lt;/b&gt; " + response.endpoints[2].grade+ "&lt;br&gt;");
                       }
                      else if ((data == 'READY' &amp;&amp; response.endpoints[0].statusMessage != 'Ready')) {
                          $("#ajaxResponse").append("&lt;b&gt;Status: &lt;/b&gt;" +response.endpoints[0].statusMessage+ "&lt;br&gt;");
                          $("#ajaxResponse").append("&lt;b&gt;Server Name: &lt;/b&gt;" + response.endpoints[0].serverName+ "&lt;br&gt;");
                          $("#ajaxResponse").append("&lt;b&gt;IP Address: &lt;/b&gt;" + response.endpoints[0].ipAddress+ "&lt;br&gt;");
                       }
                       else if (data == "DNS") {
                           $("#ajaxResponse").html("&lt;div&gt;&lt;b&gt;No cached result found.&lt;br&gt;&lt;br&gt;" +response.statusMessage+ "&lt;br&gt;..please wait a few mins and try again.&lt;/b&gt;&lt;/div&gt;");
                       }
                       else if (data == "IN_PROGRESS") {
                           $("#ajaxResponse").html("&lt;div&gt;&lt;b&gt;No cached result found.&lt;br&gt;&lt;br&gt;" +response.endpoints[0].statusMessage+ "&lt;br&gt;..please wait a few mins and try again.&lt;/b&gt;&lt;/div&gt;");
                       }
                       else if (data == "ERROR") {
                           $("#ajaxResponse").html("&lt;div&gt;&lt;b&gt;Error.&lt;br&gt;&lt;br&gt;" +response.statusMessage+ "&lt;br&gt;&lt;/b&gt;&lt;/div&gt;");
                       }
                       else {
                           $("#ajaxResponse").html("&lt;div&gt;&lt;b&gt;error&lt;/b&gt;&lt;/div&gt;");
                       }
                  },
  
              });       
      });
  
  });
  </code></pre>