出于某种原因,对新聊天消息的检查会导致更多的浏览器(以及某种程度上的服务器)负载超出我的预期。任何人都可以通过任何方式提高效率,减轻负担吗?
// Begin the cycle of refreshing the mini chat after the standard delay.
function startRefreshingMinichat(){
var secs = 30; // Chat checking frequency.
setTimeout(function (){
checkForNewChats();
startRefreshingMinichat(); // Loop the check for refresh.
}, secs*1000);
}
// Check for the latest chat and update if it's different.
function checkForNewChats(){
// Check whether the latest chat doesn't match the latest displayed chat.
// NOTE THAT THIS CALLBACK DOES NOT TRIGGER IMMEDIATELY.
$.getJSON('api.php?type=latest_chat_id&jsoncallback=?', function(data){
var newChats = false;
// Update global data stores if an update is needed.
if(updateDataStore(data.latest_chat_id, 'chat_id', 'latestChatId', 'chat_id')){
newChats = true;
}
if(newChats){ // there are new chats to show.
refreshMinichat(null, 50); // loads new chat content.
}
// Since this callback isn't immediate, any feedback has to occur whenever the callback finishes.
}); // End of getJSON function call.
}
答案 0 :(得分:1)
您可以结帐this push engine,这样您就不再需要轮询新数据了。 看看它真的很酷。
答案 1 :(得分:1)
结帐CometD。这是一个js长轮询系统,我已经成功地使用了与jQuery集成的简单聊天系统。 (上次我看,有一些jQuery特定的实现,但我从来没有找到一个足够强大的功能。)