2011-09-07 43 views
0

我正在尝试从教程中获得一个正在复制粘贴的聊天工作。由于某种原因,聊天加载功能似乎每运行一次就运行两次!这是在杀我,我无法弄清楚什么是错的。Javascript聊天多次显示相同的消息

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

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

我认为有关last_message_id它的东西,但我一直都调试运行它,它似乎知道正确的...

请帮助:(

+0

这是很多代码来通过。你能识别相关的代码片段并在此发布吗? –

+0

你用'console.log'试过了吗? – yoda

+0

无论如何,通过外观看起来'Load'方法,在'Send'方法之后立即调用,必须加载新的消息而不考虑客户端帐户。 – yoda

回答

0

通过简单地移除JS eval(result);的最重要的部分之一固定的问题大声笑...我猜jquery自己做这件事?那么它的工作...

0

更换负载

从发送与刚写到屏幕

+0

删除它并没有改变,更新了示例代码。 –

0

尝试修复你的PHP代码

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

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

不认为这是需要的,因为在此之前设置了新的last_message_id。 –

+0

你试过了吗?我认为这是因为您在SEND命令中打印了邮件,并且您还有一个显示相同邮件的计时器,因为您需要在用户发布新邮件后增加邮件计数器 –

+0

我没有做任何更改。 –

0

这可能会实现一个功能,在加载结束()函数将$ 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"); 
0

在您的代码中,if()语句查看Load()函数是否已经在进行中。 (这是在你复制的地方很好,我不知道哪里出了问题):

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 
}