dougong1031 2014-03-28 13:13
浏览 177

聊天并使滚动条设置在底部并附加新消息。

Hello all I am working on a social networking website where I have a chat box. everything is working fine in the chatbox except that I have to reload the div after every 5 seconds to get the new messages which is causing some problems for me like the same database query is going to run over again and again after every 5 seconds. and also the scroller goeson top every time the chatbox reloads i can not set it as to scroll to bottom after every 5 seconds because there may be someone just reading the messages and he will not want to scroll the box back again and again. so in short what i want to ask that is there any other way for geting the new messages in chat box . and if this is the only way can you please tell me how to code for this logic if the scroller is above 10px fromthe bottom of div scroll to bottom and append new messages other wise just append new messages.

Here is my code:

 var autoload1 = setInterval(
    function showthemess1()
    {
    $.ajax({
        type: 'POST',
        url: 'chitchatback2.php', 
        data: {'chattingwith':"<?=$nameIS2['id']?>"},
        success: function(chats){
        $("#chatboxdiv").empty().append(chats);
        } });
    }, 5000);

for making the scroller come to bottom i can do like

   $(".bechmsgbox").animate({
scrollTop: $("#chatboxdiv")[0].scrollHeight
},'slow');
  • 写回答

3条回答 默认 最新

  • dscdttg4389 2014-03-28 13:21
    关注

    My suggestion is to use sockets for this kind of situations.
    try this example as well

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题