title: Javascript/AJAX Show Text in text file automatically
body: <p>I am trying to make a simple AJAX program, that always displays the newest text from a file.
  It should be retrieving it from the file, wait 2 seconds, and if something changed, write it out.
  I tried already but the code doesn't seem to work. </p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;script type="text/javascript"&gt;
          function sleep()
          {
              var dt = new Date();
              dt.setTime(dt.getTime() + 2000);
              while (new Date().getTime() &lt; dt.getTime());
          }
          function loadXMLDoc()
          {
          var xmlhttp;
          if (window.XMLHttpRequest)
            {
            xmlhttp=new XMLHttpRequest();
            }
          xmlhttp.onreadystatechange=function()
            {
            if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200)
              {
              document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
              }
            }
          xmlhttp.open("GET","ajaxtest.txt",true);
          xmlhttp.send();
          }
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;script&gt;
      while (0=0)
       {
          loadXMLDoc();
  
      &lt;/script&gt;
          &lt;div id="myDiv"&gt;&lt;h2&gt;Status&lt;/h2&gt;&lt;/div&gt;
          &lt;script type="text/javascript"&gt;
          setTimeout(loadXMLDoc(),1000)
      }
      &lt;/script&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>How do I get it to check and update the text automatically?</p>
  
  <p>Thanks!</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Here is the final code for anybody who is interested:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;script type="text/javascript"&gt;
          function loadXMLDoc()
          {
          var xmlhttp;
          if (window.XMLHttpRequest)
            {
            xmlhttp=new XMLHttpRequest();
            }
          xmlhttp.onreadystatechange=function()
            {
            if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200)
              {
              document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
              }
            }
          xmlhttp.open("GET","status.txt",true);
          xmlhttp.send();
          }
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;script type="text/javascript"&gt;
          loadXMLDoc();
      &lt;/script&gt;
      &lt;div id="myDiv"&gt;Current Status&lt;/div&gt;
      &lt;script type="text/javascript"&gt;
          setInterval("loadXMLDoc()", 2000);
      &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>