python小菜 2017-07-02 19:20 采纳率: 0%
浏览 3

附加最后一条消息一次

Hello guys i am trying to build a chat with Jquery , php , ajax and mysql the problem that i am facing since few days is that when i get the value of the last message its keep getting append to the div what i want is to append the last message only once , and append again if there is a new message here is my ajax call

var chat = "";
     $.ajax({
            url: "php/gt_user.php",
            type: "get",
            success: function (result) {
                var gtUser = $.parseJSON(result);
                $.each(gtUser, function (idx, obj) {
                    var usrApp = "<span><i class='fa fa-user-circle-o' aria-hidden='true'></i>";
                    usrApp += "<p class='usr-name'>" + obj.Tables_in_chat + "</p></span>";
                    $('.userarea').append(usrApp);
                }); // here i get all the username who sent a message and print them on a div
                $('.userarea span').on('click', function () {
                    $('.msgarea').html("");
                    var usrName = $(this).text();
                    $('#usrname').text(usrName);
                    setInterval(function () {
                        $.ajax({
                            url: "php/admin_msg.php",
                            type: "post",
                            data: {
                                name: usrName
                            },
                            success: function (result) {
                                var lastmsg = result;

                                function appedn() {
                                    var usrMsg = "<div class='usr-msg'><i class='fa fa-user-circle-o' aria-hidden='true'></i>";
                                    usrMsg += "<span><p>" + lastmsg + "</p></span></div>";
                                    $('.msgarea').append(usrMsg);
                                }

                                if (chat !== result) {
                                    appedn();

                                } else {
                                    chat = result;
                                }

                            }

                        });
                    }, 2000);


                });
            }
        });

the respanse from php/admin_msg.php is working and i got the last message sucessfully the problem is that this script keep adding the same message to the message area , and what i want is to added the message only once if there is a new one

  • 写回答

2条回答 默认 最新

  • weixin_33736048 2017-07-02 19:29
    关注

    You need to somehow identify last message that is already appended to your html the best would be some id sent from server. So your message div should containt some data-id attribute, and then when you ask for next message get last children of $('.msgarea') element, read it data-id and compare with current one.

    Another thing I would recommend to moving to some view library or framework, react, angular, vue or whatever. It gets complicated when you want to manage such features with pure jQuery.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?