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

发布于 2024-12-03 08:08:22 字数 370 浏览 0 评论 0原文

我正在尝试让聊天工作,这是从教程中复制粘贴的。由于某种原因,聊天加载功能似乎每次应该运行一次时都运行两次!这真是要了我的命,我不知道出了什么问题。

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

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

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

请帮助:(

i am trying to get a chat working that is a copy paste from a tutorial. For some reason the chat load funcion seems to run twice everytime its supposed to run once! This is killing me, i cant figure out whats wrong.

Here is my copy paste chat: http://www.releazed.com/chat/

here is the full source code: http://myslimchatroom.wikidot.com/

I would think its something related to the last_message_id but ive been debuging it and it seems to know the right one...

Please help :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

遮了一弯 2024-12-10 08:08:22

将发送末尾的Load ) 替换

为仅写入屏幕的函数

replace Load )

from the end of send with a function that just writes to the screen

不寐倦长更 2024-12-10 08:08:22

尝试将您的 php 代码从 修复

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

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

Try to fix your php code from

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

to

 $js .= "last_message_id = $last_message_id + 1;";
鱼忆七猫命九 2024-12-10 08:08:22

这可能有效,在 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");

This might work, in the Load() function change the $post command to this:

              $.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");
久隐师 2024-12-10 08:08:22

在您的代码中,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
}

In your code, the if statement to see if the load is already in process is somehow missing from the Load() function. (It was fine in the place you copied from, I don't know what went wrong):

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
}
晌融 2024-12-10 08:08:22

通过简单地删除 JS eval(result); 的最重要部分之一解决了问题,哈哈...我猜 jquery 是自己做的?嗯,它的工作...

fixed the problem by simply removing one of the most important parts of the JS eval(result); lol... i guess jquery does this on its own? Well its working...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文