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

时间:2016-06-04 14:55:24

标签: php jquery html forms page-refresh

我有一个网站here,我希望用户能够点击聊天窗口内的链接和/或按钮,但是页面会使用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

1 个答案:

答案 0 :(得分:0)

我建议你实现一个功能,例如当用户按住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);
}