繁体   English   中英

允许用户单击自动刷新页面上的链接

[英]Allowing users to click on links on an auto refreshing page

我在这里有一个网站,我希望用户能够单击聊天窗口中的链接和或按钮,但是使用jQuery可以以很高的速度自动刷新页面,哪种最有效的方式呢?

这是我的自动刷新代码。

//Load the file containing the chat log
function loadLog(){     
    var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
    $.ajax({
        url: "chatLogs/masterlog.html",
        cache: false,
        success: function(html){        
            $("#chatbox").html(html); //Insert chat log into the #chatbox div               
            var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
            if(newscrollHeight > oldscrollHeight){
                $("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
            }               
        },
    });
}
setInterval (loadLog, 500); //Reload file every 2.5 seconds

我建议您实现一个功能,例如当用户按住CTRL键(或其他任何键)时,它会停止刷新。 当用户释放密钥时,它将再次启动。 您可以这样实现:

//Load the file containing the chat log
function loadLog(){     
    var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
    $.ajax({
        url: "chatLogs/masterlog.html",
        cache: false,
        success: function(html){        
            $("#chatbox").html(html); //Insert chat log into the #chatbox div               
            var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
            if(newscrollHeight > oldscrollHeight){
                $("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
            }               
        },
    });
}
var i = setInterval (loadLog, 500); //Reload file every 2.5 seconds

document.onkeydown = function(e){
    ctrl = e.ctrlKey;
    if(ctrl)
        clearInterval(i);
}


document.onkeydown = function(e){
    ctrl = e.ctrlKey;
    if(ctrl)
        i = setInterval(loadLog, 500);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM