title: Chat heartbeat with AJAX (PHP and JQuery)
body: <p>I have developed jquery live chat between users.</p>
  
  <p>I want to know if the heartbeat I planned is saving resources or wasting a lot?</p>
  
  <p>this is the heartbeat code:</p>
  
  <pre><code>function chatheartbeat(){
      var dataString = 'action=chatheartbeat';
      $.ajax({
          url: "../chat.php",
          type: "POST",
          data: dataString,
          success: function(items){
              if(items != 0){
                  $.each(items, function (i, elem) {
                      if ($("#chat_"+elem.from).length &gt; 0) {
                      $("#chat_"+elem.from+" .chatmessege").append('&lt;div class="chatboxmessagehead"&gt;&lt;span&gt;'+elem.from+':&lt;/span&gt;&lt;a&gt;'+elem.timee+'&lt;/a&gt;&lt;/div&gt;&lt;br&gt;&lt;div class="chatboxmessage"&gt;'+elem.msg+'&lt;/div&gt;');
                      $("#chat_"+elem.from+" .chatmessege").scrollTop($("#chat_"+elem.from+" .chatmessege")[0].scrollHeight);
                      }else{
                      startchat(elem.from);
                      }
                  });
              }
          }
      });
      setTimeout('chatheartbeat();',2000);
  }
  </code></pre>