我试图用JS做'烤面包机'通知系统,但它无法按预期运行。嵌套setTimeouts运行一次
无论何时有与用户相关的nodeJS或AJAX的更新,我想显示一个可能嵌套的通知框,如果同时存在多个通知,有点像facebook,并在5秒后被移除,独立于此刻正在运行的其他通知。
但是只有最后一个通知会消失并被删除,其余的旧通知将保持可见....我无法找到造成这种情况的原因。
这是我在mielikki对象内的代码。
var mielikki = {
notifHooks: [],
toaster : {
notifCount : 0,
launch : function(ops) {
message = '';
delay = 5000;
msgType = 'generic';
if("message" in ops) {
message = ops.message;
}
if("delay" in ops) {
delay = ops.delay;
}
if("msgType" in ops) {
msgType = ops.msgType;
}
this.notifCount += 1;
$('#toaster_template').clone().attr('id','notification-'+this.notifCount).html(message).appendTo('#notifications');
var target = $("#notification-"+this.notifCount);
var test = setTimeout(function() { target.fadeOut("slow",function() { target.remove(); })},5000);
mielikki.notifHooks.push(test);
}
}
}
div toaster_template是保留每个新通知克隆的基本div。
从任何来源或控制台会做这样的事:
mielikki.toaster.launch({'message' : 'test'});
但同样,如果前面是同时运行2+时间只有最后/最新通知将如预期做。
你看着约翰爸爸的Toastr库?这很简单,效果很好。 http://www.johnpapa.net/toastr100beta/ – Delorian 2015-04-06 01:02:13
从来没有听说过尝试和现在测试它,迄今为止这么好,谢谢!就是我正在尝试的! - 然而,为什么我的代码不能工作令人失望,我正在杀死它。 – 2015-04-06 05:33:23