Ответ 1
var that = this;
setTimeout(function() { //calls click event after a certain time
that.element.click();
}, 10000);
который работал у меня.
Я рассматриваю форму js файла Bram Jetten:
Notification.fn = Notification.prototype;
function Notification(value, type, tag) {
this.log(value, type);
this.element = $('<li><span class="image '+ type +'"></span>' + value + '</li>');
if(typeof tag !== "undefined") {
$(this.element).append('<span class="tag">' + tag + '</span>');
}
$("#notifications").append(this.element);
this.show();
}
/**
* Show notification
*/
Notification.fn.show = function() {
$(this.element).slideDown(200);
$(this.element).click(this.hide);
}
/**
* Hide notification
*/
Notification.fn.hide = function() {
$(this).animate({opacity: .01}, 200, function() {
$(this).slideUp(200, function() {
$(this).remove();
});
});
}
...
Я назначил событие click одной из моих кнопок, и когда я нажимаю эту кнопку, он вызывает новое уведомление:
new Notification('Hi', 'success');
Когда я нажимаю это уведомление, оно закрывается. Однако, если я не нажму на него через определенное время, я хочу, чтобы он был закрыт сам по себе. Как я могу назвать эту функцию скрытия или закрыть ее через некоторое время позже, когда она появилась?
var that = this;
setTimeout(function() { //calls click event after a certain time
that.element.click();
}, 10000);
который работал у меня.
Установите тайм-аут и принудительно спрячьте.
/**
* Show notification
*/
Notification.fn.show = function() {
var self = this;
$(self.element).slideDown(200)
.click(self.hide);
setTimeout(function() {
self.hide();
// 3000 for 3 seconds
}, 3000)
}
Смените строки на
Notification.fn.show = function() {
var self=this;
$(this.element).slideDown(200);
$(this.element).click(this.hide);
setTimeout(function(){
self.hide();
},2000);
}
но вам понадобится дополнительное внутреннее логическое значение, так что вы не сможете скрыть (и therfor destroy) уведомление дважды.
Notification.fn.hide = function() {
if (!this.isHidden){
var self=this;
$(this).animate({opacity: .01}, 200, function() {
$(this).slideUp(200, function() {
$(this).remove();
self.isHidden=true;
});
});
}
}
вызывает click event
через определенное время
setTimeout(function() { //calls click event after a certain time
$(".signature-container .nf-field-element").append( $('#signature-pad'));
}, 10000);