title: PHP infinitive loop or jQuery setInterval?
body: <p>Js:</p>
  
  <pre><code>&lt;script&gt;
  function cometConnect(){
      $.ajax({
            cache:false,
            type:"post",
            data:'ts='+1,
            url: 'Controller/chatting',
            async: true,
            success: function (arr1) {
                $(".page-header").append(arr1);
            },
            complete:function(){
              cometConnect(true);
              nerr=false;
            },
            dataType: "text"
          }); 
  }
  cometConnect();
  &lt;/script&gt;
  </code></pre>
  
  <p>Php:</p>
  
  <pre><code>public function chatting()
  {
      while(true)
      {
         if(memcache_get(new_message))
            return new_message; 
         sleep(0.5);
      }
  }
  </code></pre>
  
  <p>Is this a better solution than setting setInterval which connects to the PHP method which returns message if there is any every 1 second (1 sec increases +0.25 every 5 seconds let's say)? </p>
  
  <p>If I used first solution, I could probably use sleep(0.5) it would give me messages instantly, because php loop is cheap, isn't? </p>
  
  <p>So, what solution is better (more importantly, which takes less resources?). Because there are going to be hundreds of chats like this.</p>
  
  <p>Plus, can first solution cause problems? Let's say I would reload a page or I would stop execution every 30 secs so I wouldn't get 502 Bad Gateway.</p>
  
  <p><strong>EDIT:</strong> I believe the second solution is better, so I am going to reimplement my site, but I am just curious if this can cause problems to the user or not? Can something not expected happen?
  First problem I noticed is that you can't go to other page until there is at least one new message.</p>