由於某種原因,這個檢查新的聊天消息會導致瀏覽器的數量(以及某種程度上的服務器)的負載超出我的預期。任何人都可以看到我可以提高效率的方法來減輕負載?如何讓我的JavaScript聊天輪詢腳本更高效?
// 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.
}
var secs = 1; :o) – jAndy 2010-05-06 13:06:30
沒錯。 :我想我應該指出這是問題的一部分。我很樂意提供更快的檢查時間來更頻繁地更新聊天記錄,但它似乎使瀏覽器緊縮得有點過分。 (並且我承擔了對服務器負載的影響,儘管我本身無法衡量) – Kzqai 2010-05-06 15:05:53