Good community. I'm working on this very simple chat system.
I do not understand why I do not get the messages in real time. I can only receive messages from another user if I close and back open the div, then the message already appears.
How can I solve it? As I'm not very comfortable with ajax I ask for your help.
I think the problem will be in the last function, message cycle:
function message_cycle()
{
$.ajax({
url:'chat.class.php',
type:'POST',
data:'unread=true',
dataType:'JSON',
success:function(data){
$.each(data , function( index, obj ) {
var user = index;
var box = $("#jd-chat").find("div#2").parents(".jd-user");
$(".jd-online").find(".light").hide();
$.each(obj, function( key, value ) {
if($.inArray(user,open) !== -1 )
$(box).find(".jd-body").append("<span style='display:block' class='other'> " + value + "</span>");
else
snd.play();
$(".jd-online").find("span#" + user + " .light").show();
});
});
}
});
}
setInterval(message_cycle,1000);
});
Any ideas?
I found out where the problem was, instead of the div id = 2
var box = $("#jd-chat").find("div#2").parents(".jd-user");
I called for the span
that receives the user sender and user receiver text
var box = $("#jd-chat").find(".me").parents(".jd-user");
It now receives the messages and immediately shows the messages that exist without having to close and open the div