Имеется div c id "chat-messages"
В него постепенно из бд добавляются div'ы с id "message-ID СООБЩЕНИЯ"
Пример:
<div id="chat-messages">
<div id="message-1">Сообщение 1</div>
<div id="message-2">Сообщение 2</div>
<div id="message-3">Сообщение 3</div>
</div>
Надо удалять все старые дивы так, чтобы в блоке chat-messages было максимум 60 сообщений
JS:
UMBRELLA.modules.jchat.js = {
messages_id: null,
div_reload: true,
tmp: {
ci: '<div class="mainChat mainNew" id="mainChat"></div>',
cm: [
'<div id="subChat-closebar" onclick="UMBRELLA.modules.jchat.js.close()">Скрыть чат (Фонд: <span id="jchat-fond"></span>)</div><div id="subChat-messages"><span id="jchat-message"></span></div><input type="text" id="JChat-message" onkeyup="UMBRELLA.modules.jchat.js.keyboard(event.keyCode)" maxlength="300" style="width: 406px; left: -5px; position: relative;"><span class="f-buttons" style="left: -2px; position: relative;"> <span class="f-bu f-bu-success" onclick="UMBRELLA.modules.chat.postmsg()"><input type="button" class="chatSprite" id="chs-30"> Сказать</span> <span class="f-bu"><input id="chs-31" class="chatSprite" type="button"> Бар</span></span>',
'<div id="subChat-closebar" onclick="UMBRELLA.modules.jchat.js.open()">Показать чат</div>'
]
},
fa: {
fp: function() {
$('body').append(UMBRELLA.modules.jchat.js.tmp.ci);
$('#mainChat').append(UMBRELLA.modules.jchat.js.tmp.cm[0]);
return true;
}
},
keyboard: function(kid) {
if (kid == 13) UMBRELLA.modules.jchat.js.action.post();
},
close: function() {
UMBRELLA.modules.jchat.js.div_reload = false;
$(".mainChat").html(UMBRELLA.modules.jchat.js.tmp.cm[1]);
},
open: function() {
UMBRELLA.modules.jchat.js.div_reload = true;
UMBRELLA.modules.jchat.js.messages_id = 0;
$(".mainChat").html(UMBRELLA.modules.jchat.js.tmp.cm[0]);
UMBRELLA.modules.javax.call($.toJSON({
request: "chat",
type: "getChats",
id: UMBRELLA.modules.jchat.js.messages_id
}));
},
calls: {
make: function(a) {
if (UMBRELLA.modules.jchat.js.fa.fp()) {
a();
$.cookie('tmp_jchat_rt', 0);
}
},
jchat: function(a) {
var c = JSON.parse(a);
if (c.request == 'chat') {
if (c.type == 'jchaterror') {
if (c.errid == '001') {
UMBRELLA.plugins.mainWindow.message('Ошибка!', 'Заполните поле сообщения перед отправкой.');
} else if (c.errid == '002') {
UMBRELLA.plugins.mainWindow.message('Ошибка!', 'Вы отключены от чата на ' + c.time + ' минут. Причина: ' + c.reason + '.');
}
}
if (c.type == 'chats') {
$("#jchat-fond").text(c.fond);
if (!c.messages) {
return false;
}
for (var i = 0; i < c.messages.length; i++) {
UMBRELLA.modules.jchat.js.messages_id = c.messages[i].id;
$("#jchat-message").prepend('<div class="subChat-message" id="jchat-' + c.messages[i].id + '" ' + (c.messages[i].touser[0] == MY.id ? 'style="background: rgba(255,255,255, 0.1);border: solid 1px #093406;"' : '') + '><span>' /* */ + (c.messages[i].system == 3 ? '<font color="#32CD32"><b>' + c.messages[i].message + '</b></font>' : (c.messages[i].system == 2 ? '<span><b><div class="f-bwi"><img class="f-bwi-pic" width="84" height="85" alt="Gold" src="/img/1353213729.png"></img><div class="f-bwi-text"><input id="spriteRank' + c.messages[i].user[0] + '" class="sprite" type="button"></input> <b>' + c.messages[i].message + '</b><br>Поймал бонусный ящик с 100 еденицами опыта.</div></div></b></span>' : (c.messages[i].system == 0 ? /* */ (c.messages[i].pm != 0 ? '[PM]' : '') + '<input style="box-shadow: none; background-color: transparent;" type="button" class="sprite" id="spriteRank' + c.messages[i].user[0] + '" /><b>' + c.messages[i].user[1] + '</b></span>' + (c.messages[i].touser.length != 0 ? ' → <span><input style="box-shadow: none; background-color: transparent;" type="button" class="sprite" id="spriteRank' + c.messages[i].touser[0] + '" /><b>' + c.messages[i].touser[1] + '</b>:</span> ' : ': ') + c.messages[i].message /**/ : 'Наблюдатель'))) /**/ + '</div>');
}
}
}
},
open: function(a) {
if (a[0] == 'chat') {
var b = (function() {
UMBRELLA.modules.javax.call($.toJSON({
request: "chat",
type: "getChats",
id: "0"
}))
});
}
UMBRELLA.modules.jchat.js.calls.make(b);
},
refresh: function(a) {
if (UMBRELLA.modules.jchat.js.div_reload != false) {
if (a != 'post') {
if ($.cookie('tmp_jchat_rt') < Math.floor((new Date()).getTime() / 1000)) {
UMBRELLA.modules.javax.call($.toJSON({
request: "chat",
type: "getChats",
id: UMBRELLA.modules.jchat.js.messages_id
}));
}
} else {
UMBRELLA.modules.javax.call($.toJSON({
request: "chat",
type: "getChats",
id: UMBRELLA.modules.jchat.js.messages_id
}));
}
}
}
},
action: {
post: function() {
var message = $("#JChat-message").val();
$("#JChat-message").attr("value", "");
var b = UMBRELLA.modules.javax.call($.toJSON({
request: "chat",
type: "setMessages",
text: message
}));
UMBRELLA.modules.jchat.js.calls.refresh('post');
$.cookie('tmp_jchat_rt', Math.floor((new Date()).getTime() / 1000) + 1500);
}
}
};
$(document).ready(function() {
setInterval(function() {
UMBRELLA.modules.jchat.js.calls.refresh()
}, 5000);
});