javascript - get message without re open div -
im working in chat system simple. have problem. can other user messages if close , re open chat conversation div, message appears.
how can solve this? problem should in piece of code. not comfortable ajax ask help.
js:
$(document).ready(function(){ var snd = new audio("images/new_msg.wav"); var open=array(); $("#jd-chat .jd-online_user").click(function(){ var user_name = $.trim($(this).text()); var id = $.trim($(this).attr("id")); if($.inarray(id,open) !== -1 ) return open.push(id); $("#jd-chat").prepend('<div class="jd-user">\ <div class="jd-header" id="' + id + '">' + user_name + '<span class="close-this"> x </span></div>\ <div class="jd-body"></div>\ <div class="jd-footer"><input id="textareabox" placeholder="escrever..."></div>\ </div>'); $.ajax({ url:'chat.class.php', type:'post', data:'get_all_msg=true&user=' + id , success:function(data){ $("#jd-chat").find(".jd-user:first .jd-body").append("<span style='display:block' class='me'> " + data + "</span>"); } }); }); $("#jd-chat").delegate(".close-this","click",function(){ removeitem = $(this).parents(".jd-header").attr("id"); $(this).parents(".jd-user").remove(); open = $.grep(open, function(value) { return value != removeitem; }); }); $("#jd-chat").delegate(".jd-header","click",function(){ var box=$(this).parents(".jd-user,.jd-online"); $(box).find(".jd-body,.jd-footer").slidetoggle(); }); $("#search_chat").keyup(function(){ var val = $.trim($(this).val()); $(".jd-online .jd-body").find("span").each(function(){ if ($(this).text().search(new regexp(val, "i")) < 0 ) { $(this).fadeout(); } else { $(this).show(); } }); }); $("#jd-chat").delegate(".jd-user input","keyup",function(e){ if(e.keycode == 13 ) { var $cont = $('.jd-body'); var box=$(this).parents(".jd-user"); var msg=$(box).find("input").val(); var = $.trim($(box).find(".jd-header").attr("id")); $.ajax({ url:'chat.class.php', type:'post', data:'send=true&to=' + + '&msg=' + msg, success:function(data){ $('#textareabox').val(''); $(box).find(".jd-body").append("<span style='display:block' class='me'> " + msg + "</span>"); $cont[0].scrolltop = $cont[0].scrollheight; $cont.append('<span>' + $(this).val() + '</span>'); $cont[0].scrolltop = $cont[0].scrollheight; $(this).val(''); } }); } }); 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); });
how messages displayed when first open chat conv div ?
i'm assuming message_cycle() supposed display new messages in div every second, suppose problem here...
i'm not confortable :
var box = $("#jd-chat").find("div#2").parents(".jd-user");
a div can't have id starting number, need <div id="chat2">
instead of <div id="2">
.
after line add console.log('box found : '+box.length) ;
make sure box correctly found in message_cycle.
Comments
Post a Comment