[英]Javascript chat showing same messages multiple times
我正在尝试使聊天工作正常进行,该工作是教程中的复制粘贴。 由于某种原因,聊天加载功能似乎每次应该运行一次就运行两次! 这真杀了我,我不知道怎么了。
这是我的复制粘贴聊天: http : //www.releazed.com/chat/
这是完整的源代码: http : //myslimchatroom.wikidot.com/
我认为它与last_message_id有关,但是我一直在调试它,而且似乎知道正确的方法...
请帮忙 :(
替换Load )
从发送的末尾开始,带有仅写入屏幕的功能
尝试从修复您的php代码
$js .= "last_message_id = $last_message_id;";
至
$js .= "last_message_id = $last_message_id + 1;";
这可能起作用,在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");
在您的代码中,以某种方式缺少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
}
通过简单地删除JS eval(result);
的最重要部分之一来解决该问题eval(result);
大声笑...我猜jquery自己做吗? 好吧...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.