我有一个简单的聊天工具来更新聊天室:
setInterval (loadLog, 2500);
function loadLog(){
//Scroll height before the request
var oldScrollHeight = document.getElementById("chatMessages").scrollHeight - 20;
$.ajax({
url: "/includes/chat/log.html",
cache: false,
success: function(html){
//Insert chat log into the #chatMessages div
$("#chatMessages").html(html);
var newScrollHeight = document.getElementById("chatMessages").scrollHeight - 20;
if(newScrollHeight > oldScrollHeight){
//Autoscroll to bottom of div
$("#chatMessages").animate({scrollTop: newScrollHeight}, 'normal');
}
},
});
}可靠地每2.5秒执行一次。但是我想节省带宽所以..。
我改变:
cache: false至
cache: true现在,它不能每2.5秒可靠地执行一次。(可能以后的每个请求都比以前的要花更长的时间?)也许不是,它的行为很奇怪)
我的研究没有取得任何成果。请帮帮忙!<3
发布于 2015-11-29 16:19:42
如果启用缓存,浏览器将缓存来自先前对服务器的调用的响应,并且它不会进行后续调用。这是预期的,这也是设计这个cache: true属性的目的。如果您想减少带宽使用,您可以考虑使用推送技术,而不是定期轮询。这可以使用HTML5 WebSockets来实现。在这种情况下,每当发生更新时,服务器将向客户端推送通知,而不是每2.5秒对服务器轮询一次。显然,这将只在支持WebSockets的浏览器中工作,因此,如果需要支持遗留浏览器,则可能需要在使用它们之前进行特性检测。
https://stackoverflow.com/questions/33984965
复制相似问题