title: Calling ajax function continuously at a specific interval until a condition is met?
body: <p>I have two jsp pages. One is UI and another is the jsp which gets response. </p>
  
  <p><strong>UICounter.jsp</strong>:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
  &lt;title&gt;Page&lt;/title&gt;
  &lt;script&gt;
  function getXMLHttpRequest() {
        var xmlHttpReq = false;
        // to create XMLHttpRequest object in non-Microsoft browsers
        if (window.XMLHttpRequest) {
          xmlHttpReq = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
          try {
            // to create XMLHttpRequest object in later versions
            // of Internet Explorer
            xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
          } catch (exp1) {
            try {
              // to create XMLHttpRequest object in older versions
              // of Internet Explorer
              xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (exp2) {
              xmlHttpReq = false;
            }
          }
        }
        return xmlHttpReq;
      }
  
  function makeRequest() {
        var xmlHttpRequest = getXMLHttpRequest();
        xmlHttpRequest.onreadystatechange = getReadyStateHandler(xmlHttpRequest);
        xmlHttpRequest.open("POST", "Control.jsp", true);
        xmlHttpRequest.setRequestHeader("Content-Type",
            "application/x-www-form-urlencoded");
        xmlHttpRequest.send(null);
      }
  
  function getReadyStateHandler(xmlHttpRequest) {
        // an anonymous function returned
        // it listens to the XMLHttpRequest instance
        return function() {
          if (xmlHttpRequest.readyState == 4) {
            if (xmlHttpRequest.status == 200) {
                var gotCount = parseInt(xmlHttpRequest.responseText);
                      document.getElementById("counterLabel").innerHTML = gotCount;
  
            } else {
              alert("HTTP errorrr " + xmlHttpRequest.status + ": " + xmlHttpRequest.statusText);
            }
          }
        };
      }
  
  &lt;/script&gt;   
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="counterLabel"&gt;0 &lt;/div&gt;
  &lt;script type="text/javascript"&gt;makeRequest();&lt;/script&gt;  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>Control.jsp</strong>:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
  &lt;title&gt;Insert title here&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;%
  if(session.getAttribute("counter") != null &amp;&amp; !session.getAttribute("counter").toString().equals(""))
  {
      String stringStatus = session.getAttribute("counter").toString().trim();
      int statusCounter = Integer.parseInt(stringStatus);
      session.setAttribute("counter",String.valueOf(++statusCounter));
  
  }else{
      session.setAttribute("counter",String.valueOf(1));
      out.print(session.getAttribute("counter"));
  }
  %&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I want to make ajax calls until a condition (the result is 100) is met. I have tried calling same function (<code>makeRequest()</code>) in result of the ajax function while checking till result is 100, but it's not working. Can someone please suggest how to achieve this, example is appreciated? Please let me know if any further clarification is needed.</p>