gpt4 book ai didi

javascript - 如何使我的 javascript 聊天轮询脚本更加高效?

转载 作者:行者123 更新时间:2023-12-02 20:38:59 25 4
gpt4 key购买 nike

出于某种原因,对新聊天消息的检查会导致浏览器(以及在某种程度上服务器)负载比我预期的要大。有人看到我可以提高效率、减轻负载的任何方法吗?

// 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.
}

最佳答案

查看CometD 。这是一个 js 长轮询系统,我在与 jQuery 集成的简单聊天系统中取得了一些成功。 (上次我查看时,有一些 jQuery 特定的实现,但我从未找到一个对我来说足够强大的实现。)

关于javascript - 如何使我的 javascript 聊天轮询脚本更加高效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2781245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com