Javascript chat showing same messages multiple times
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 :(
replace Load )
from the end of send with a function that just writes to the screen
Try to fix your php code from
$js .= "last_message_id = $last_message_id;";
to
$js .= "last_message_id = $last_message_id + 1;";
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");
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
}
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...
精彩评论