【问题标题】:Javascript chat showing same messages multiple timesJavascript 聊天多次显示相同的消息
【发布时间】:2011-11-12 10:47:43
【问题描述】:

我正在尝试让聊天工作正常进行,它是教程中的复制粘贴。由于某种原因,聊天加载功能似乎每次运行一次时都会运行两次!这要死我了,我不知道怎么回事。

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

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

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

请帮忙:(

【问题讨论】:

  • 这需要大量代码。能找出相关的sn-p代码贴在这里吗?
  • 你试过用console.log调试吗?
  • 无论如何,从外观上看,Load 方法似乎是在 Send 方法之后立即调用的,必须在不考虑客户帐户的情况下加载新消息。
  • 删除它并没有改变,更新了示例代码。

标签: php javascript mysql ajax chat


【解决方案1】:

替换加载

从发送的末尾开始,使用一个只写入屏幕的函数

【讨论】:

    【解决方案2】:

    尝试修复您的 php 代码

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

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

    【讨论】:

    • 不要认为这是需要的,因为在此之前设置了新的 last_message_id。
    • 你试过了吗?我认为这是因为您在 SEND 命令中打印了消息,并且您还有显示相同消息的计时器,因为您需要在用户发布新消息后增加消息计数器
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-01
          • 1970-01-01
          • 2021-09-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多