Javascript聊天多次显示相同的消息

时间:2011-09-07 19:12:06

标签: php javascript mysql ajax chat

我正在尝试进行聊天工作,这是教程中的复制粘贴。出于某种原因,聊天加载功能似乎每次运行一次就会运行一次!这让我很难过,我无法弄清楚什么是错的。

以下是我的复制粘贴聊天:http://www.releazed.com/chat/

这里是完整的源代码:http://myslimchatroom.wikidot.com/

我认为它与last_message_id有关但我一直在调整它,它似乎知道正确的...

请帮助:(

5 个答案:

答案 0 :(得分:0)

替换加载

从发送结束时带有一个只写入屏幕的功能

答案 1 :(得分:0)

尝试修复您的PHP代码

 $js .= "last_message_id = $last_message_id;";

 $js .= "last_message_id = $last_message_id + 1;";

答案 2 :(得分:0)

这可能有效,在Load()函数中将$ post命令更改为:

              $.post("ajax.php",
        {
              act: "load", // point out that it is downloading messages
              last: last_message_id, // pass number of the last message that the user was last boot
              rand: (new Date()).getTime()
        },
           function (result) { // this function as a parameter is passed javascript code, which we must comply
                eval(result); // execute script from the server
                $(".chat").scrollTop($(".chat").get(0).scrollHeight); // scrolls the message down
                load_in_process = false; // say that downloading is over, we can now start a new download
        }, 
       "text");

答案 3 :(得分:0)

在你的代码中,在Load()函数中以某种方式缺少if语句来查看加载是否已经在进行中。 (你复制的地方很好,我不知道出了什么问题):

function Load() {
    // Check whether we can download the message. This is done to ensure that we do not start the download again, if the old download is not over yet.
    if(!load_in_process) //this line was missing
    { //also missing
            load_in_process = true; // Loading started
            // refer the request to the server, which returns us javascript
            $.post("ajax.php",
            {
                  act: "load", // point out that it is downloading messages
                  last: last_message_id, // pass number of the last message that the user was last boot
                  rand: (new Date()).getTime()
            },
               function (result) { // this function as a parameter is passed javascript code, which we must comply
                    eval(result); // execute script from the server
                    $(".chat").scrollTop($(".chat").get(0).scrollHeight); // scrolls the message down
                    load_in_process = false; // say that downloading is over, we can now start a new download
            });
   } //also missing
}

答案 4 :(得分:0)

通过简单地删除JS eval(result); lol中最重要的部分之一来修复问题...我猜jquery自己做了吗?那它的工作......