开发者

how do I make shoutbox chat-log scroll to bottom with constant refreshing without changing mouse focus?

开发者 https://www.devze.com 2023-02-28 15:11 出处:网络
I am writing a small shoutbox/chat app where I am using the following piece of code to refresh the message log div every 0.5 seconds and to refresh to the bottom to show the latest message.

I am writing a small shoutbox/chat app where I am using the following piece of code to refresh the message log div every 0.5 seconds and to refresh to the bottom to show the latest message.

$(setInterval(function() {
    $('.messageLog').load('display_messages.php');
    window.location='#bottom';
}, 500));

the '#bottom' refers to a empty div I have put at the end of the '.messageLog' div so I have a location to refer to as the bottom of the message log. The problem with this method is, when I am trying to login (by simply typing in a username and pressing the sign-in button through a small form at the top of the chatbox) the username input field keeps getting refreshed and put out of focus every 0.5 seconds in firefox and internet explorer (works fine in chr开发者_如何学JAVAome). The problem turns out to be the line "window.location='#bottom'", which is causing the input field focusing problem.

I was wondering if there was a way around this so that I can still refresh the message log every 0.5 seconds and automatically scroll to the bottom while not interfere with users trying to sign in through the username field.


remove the anchor, use this instead

$(".messageLog").attr({ scrollTop: $(".messageLog").attr("scrollHeight") });
0

精彩评论

暂无评论...
验证码 换一张
取 消