title: ajax - How can I build a chat with Ajax and settimeout without the process taking a lot of virtual memory?
body: <p>I'm trying to build a chat based on AJAX that receives data every second by settimeout.
  I wrote a basic code where there is a number that increases each second of the number obtained from the php page2.
  I checked it from my computer by opening the file in 3 different browsers and saw that it takes a lot of virtual memory from the server. My fear is what will happen if I build a real chat in this format and 50 people simultaneously enter?
  I know that setTimeout takes a lot of memory, is there any other way to build chat? Or can I improve what I did?
  I would be very happy to answer!</p>
  
  <p>that's what i did:</p>
  
  <pre><code>&lt;body&gt;
  &lt;h1 id='number'&gt;0&lt;/h1&gt;
  
  &lt;script&gt;
  function ajax() {
   if (window.XMLHttpRequest) {
    var a = new XMLHttpRequest()
   } else {
    var a = new ActivXObject('Microsoft.XMLHTTP')
   }
   return a
  }
  
  var settime = 1000;
  var myTime;
  
  ajaxMain();
  
  function ajaxMain() {
   xmlhttp = ajax();
   xmlhttp.onreadystatechange = function() {
    if (((xmlhttp.readyState == 4) || (xmlhttp.readyState == 0)) &amp;&amp; (xmlhttp.status == 200)) {
     if (xmlhttp.responseText) {
      id('mmm').innerHTML = (xmlhttp.responseText) * 1 + (id('number').innerHTML) * 1;
      myTime = setTimeout('ajaxMain()', settime)
     }
    } else {
     if (xmlhttp.responseText.match(/^\&lt;\!/)) {
      clearTimeout(myTime);
      myTime = setTimeout('ajaxMain()', settime)
     }
    }
   }
   xmlhttp.open('GET', 'page2.php', true)
   xmlhttp.send()
  }
  
  &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>page2.php:</p>
  
  <pre><code>&lt;?php
  
  echo 1
  ?&gt;
  </code></pre>