我开发了用户之间的 jquery 实时聊天。
我想知道我计划的心跳是节省资源还是浪费很多?
这是心跳代码:
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 > 0) {
$("#chat_"+elem.from+" .chatmessege").append('<div class="chatboxmessagehead"><span>'+elem.from+':</span><a>'+elem.timee+'</a></div><br><div class="chatboxmessage">'+elem.msg+'</div>');
$("#chat_"+elem.from+" .chatmessege").scrollTop($("#chat_"+elem.from+" .chatmessege")[0].scrollHeight);
}else{
startchat(elem.from);
}
});
}
}
});
setTimeout('chatheartbeat();',2000);
}