I've looked some AJAX threads looking for my problem, but I didn't find a solution for my case. Here's the thing:
I have a page with user comments. Those comments are saved in a database and every time the user press a "Refresh" button there's a connection to the database, getting the comments in response. If there are any new messages, there's a div "There are _ new messages". However, this last message never shows up properly, because it appears before the messages are loaded.
Here's the code:
AJAX function
function GetComments()
{
$("#cont-comment").empty(); //div that contains the messages
$.ajax(
{
url: "get.php",
dataType: "json",
success: function(data)
{
n_msg=data.length;
for (i=0; i<n_msg; i++)
{
nick=data[i].nick;
msg=data[i].msg;
WriteNewComment(nick,msg);
num_comments=document.getElementsByClassName("comment-msg").length;
}
}
});
}
And here's the problem.
$("#refresh").click(function()
{
num_comments_before=num_comments;
GetComments();
alert("done");
});
The "done" message appears before the new comments. I would like that the message "done" only would appear when the new comments show up - when AJAX has finished.