这是执行我父事件的整个函数(使用json)。
function message_action(){
function tipsy(){
$("div#box_message > div > div.slimScrollDiv > div > ul > li, div.messagePopup > div.nav > div.name > a, div.messagePopup > div.box > div.content > ul > li > div.message_avatar, div.messagePopup > div.box > div.content > ul > li.user > div.message_text").tipsy({
html: true,
gravity: 'e',
});
$("div.messagePopup > div.box > div.content > ul > li.guy > div.message_text").tipsy({
html: true,
gravity: 'w',
});
}
tipsy();
function sound(){
if($("audio.sounds-notification").length != 0){
$("audio.sounds-notification").remove();
$.playSound_notification('../data/sounds/notification');
}else {
$.playSound_notification('../data/sounds/notification');
}
}
function view(id){
var div = $("div.messagePopup[data-id="+id+"]");
div.mouseover(function(){
if(div.hasClass('see') == false){
$.post('../function/message',{view:id},function(){
div.addClass('see');
});
}
});
}
function message(){
$.post('../function/message',{message:'friend'},function(data){
var ul = $("div#box_message > div#friend_message > div.slimScrollDiv > div.friend_box_content > ul");
for(var i in data){
rows = data[i];
ul.append(getCode(rows.avatar_img, rows.fullname, rows.guy_id, rows.username));
tipsy();
}
open();
search();
},'json');
function search(){
var input = $("div#box_right > div#box_message > div#option_message > div.search > input");
input.keyup(function(){
var keyword = input.val();
if(keyword == ''){
$("div#box_right > div#box_message > div#search_message").slideUp('fast');
$("div#box_right > div#box_message > div#friend_message").slideDown('fast');
}else {
$("div#box_right > div#box_message > div#search_message").slideDown('fast');
$("div#box_right > div#box_message > div#friend_message").slideUp('fast');
$.post('../function/message',{search:keyword},function(data){
console.log(data);
var ul = $("div#box_right > div#box_message > div#search_message > div.slimScrollDiv > div > ul");
ul.html('');
if(data != ''){
for(var i in data){
var rows = data[i];
var li = ul.children('li');
if(li.length == 0){
ul.append(getCode(rows.avatar_img, rows.fullname, rows.guy_id, rows.username));
}else {
li.after(getCode(rows.avatar_img, rows.fullname, rows.guy_id, rows.username));
}
open();
}
}else {
ul.html("<li class='note_nodata'><a>Không tìm thấy người này</a></li>");
}
},'json');
}
});
}
function getCode(avatar_img, fullname, guy_id, username){
code = '<li data-id="'+guy_id+'" class="east" quick-title="@'+username+'"><div class="avatar"><a href="../wall/'+username+'" style="background-image: url('+avatar_img+');"></a></div><div class="name" style="border-color: transparent"><a>'+fullname+'</a></div></li>';
return code;
}
}
message();
function open(){
var li = $("div#box_message > div > div.slimScrollDiv > div > ul > li");
li.click(function(){
var id = $(this).attr('data-id');
popup(id);
});
}
function popup(id){
var div = $("div.messagePopup[data-id='"+id+"']");
if(div.length == 0){
$.post('../function/message',{show:id},function(data){
console.log(data);
if(data.view == 'see'){
div.addClass('see');
}
},'json');
code = '<div class="messagePopup" data-id="'+id+'"><div class="nav"><div class="name"><a>'+name+'</a></div><div class="button"><div class="close"><a></a></div></div></div><div class="box"><div class="content"><ul></ul></div><div class="type"><textarea type="text" placeholder="Viết tin nhắn"></textarea></div></div></div>';
$("div#giccos").after(code);
var div = $("div.messagePopup[data-id='"+id+"']");
$("div.messagePopup[data-id='"+id+"'] > div.nav > div.name").append('<div id="loading" class="loading_data_w" style="width: 20px;display: inline-block;vertical-align: top;margin-top: 10px;margin-left: 6px;"><i></i></div>');
div.addClass('new');
var div_new = $("div.messagePopup[data-id='"+id+"'].new");
div_new.message();
getData(id,'','','','false');
status(id,'',30000);
load(id);
check(id);
post(id);
view(id);
div_new.removeClass('new');
}
}
function newPopup(){
setTimeout(function(){
$.post('../function/message',{check:''},function(data){
console.log(data);
for(var i in data){
var rows = data[i];
var id = rows.user_id;
var div = $("div.messagePopup");
if(id != null && div.length == 0){
popup(id);
sound();
}
}
},'json');
newPopup();
},'6000');
}
newPopup();
function getData(id,type,message_id,timeout,notification){
$.post('../function/message',{data:id,type:type,message_id:message_id},function(data){
console.log(data);
var popup = $("div.messagePopup[data-id='"+id+"']");
var div = $("div.messagePopup[data-id='"+id+"'] > div.box > div.content");
var ul = div.children('ul');
for(var i in data){
var rows = data[i];
if(type == 'old'){
var ulHeight_old = ul.height();
ul.children('li[message-id="'+message_id+'"]').before(getCode(rows.avatar_img, rows.content, rows.message_id, rows.time, rows.user, ''));
ulHeight = ul.height();
div.scrollTop(ulHeight - ulHeight_old);
}if(type == 'new'){
ul.append(getCode(rows.avatar_img, rows.content, rows.message_id, rows.time, rows.user, ' new'));
ulHeight = ul.height();
div.scrollTop(ulHeight);
ul.children('li.new').fadeOut(function(){
$(this).fadeIn().removeClass('new');
});
popup.removeClass('see');
}else if(type == ''){
ul.append(getCode(rows.avatar_img, rows.content, rows.message_id, rows.time, rows.user, ''));
ulHeight = ul.height();
div.scrollTop(ulHeight);
}
tipsy();
if(notification == 'true'){
sound();
}
}
$("div.messagePopup[data-id='"+id+"'] > div.nav > div.name > div#loading").fadeOut(function(){
$(this).remove();
});
if(type == 'old' && data == null){
div.addClass('null');
ul.children('div#loading').slideUp(function(){
$(this).remove();
});
}
},'json');
function getCode(avatar_img, content, message_id, time, user, type){
if(user == 'user'){
code = '<li class="user'+type+'" message-id="'+message_id+'"><div class="message_text east" quick-title="<span>Gửi lúc '+time+'</span>"><div>'+content+'</div><i></i></div></li>';
}else if(user == 'guy'){
code = '<li class="guy'+type+'" message-id="'+message_id+'"><div class="message_avatar"><img src="'+avatar_img+'" /></div><div class="message_text west" quick-title="<span>Gửi lúc '+time+'</span>"><i></i><div>'+content+'</div></div></li>';
}
return code;
}
}
function status(id,color,timeout){
$.post('../function/message',{status:id},function(data){
console.log(data);
var nav = $("div.messagePopup[data-id='"+id+"'] > div.nav");
var name = nav.children('div.name').children('a');
if(name.text() == ''){
name.text(data.name);
}
if(data.status == 'offline'){
nav.css('background-color','#75797C');
}else {
nav.css('background-color','#3A6A99');
}
},'json');
if(timeout != ''){
setTimeout(function(){
status(id,color,timeout);
},timeout);
}
}
function load(id){
var div = $("div.messagePopup[data-id='"+id+"'] > div.box > div.content");
div.scroll(function(){
if($(this).scrollTop() == 0 && div.hasClass('null') == false){
var message_id = $("div.messagePopup[data-id='"+id+"'] > div.box > div.content > ul > li[message-id]:first").attr('message-id');
var loading = $("div.messagePopup[data-id='"+id+"'] > div.box > div.content > ul > div.loading_data_b");
if(loading.length == 0){
$('li[message-id="'+message_id+'"]').before('<div id="loading" class="loading_data_b" style="padding: 10px;"><i></i></div>');
}
getData(id,'old',message_id,'','false');
}
});
}
function check(id){
setTimeout(function(){
var div = $("div.messagePopup[data-id='"+id+"']");
if(div.length != 0){
var message_id = $("div.messagePopup[data-id='"+id+"'] > div.box > div.content > ul > li[message-id]:last").attr('message-id');
getData(id,'new',message_id,'','true');
check(id);
}
},'4000');
}
function post(id){
var input = $("div.messagePopup[data-id='"+id+"'] > div.box > div.type > textarea");
$(input).keypress(function(event){
var keycode = (event.keyCode ? event.keyCode : event.which);
if(keycode == '13'){
var input = $(this);
var val = input.val();
var message_id = $("div.messagePopup[data-id='"+id+"'] > div.box > div.content > ul > li[message-id]:last").attr('message-id');
if(val != ''){
$.post('../function/message',{post:val,id:id,},function(data){
input.val('');
getData(id,'new',message_id,'','false');
});
}
}
});
}
}
message_action();
整个功能很好,因为函数newPopup()返回一个值。此时它将调用函数popup()返回id。但是函数popup()中调用的函数没有实现。为什么呢?