Skip to content

Instantly share code, notes, and snippets.

@gnarf
Forked from ryanneufeld/gist:1323944
Created October 29, 2011 01:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnarf/1323953 to your computer and use it in GitHub Desktop.
Save gnarf/1323953 to your computer and use it in GitHub Desktop.
//based on https://gist.github.com/07a297472f182f7a7132/79d3dea1e3dc7ce2d065a42316b83ec820671634
;(function($, undefined) {
$.notify = function(options) {
if(options.timeOut > 0)
{
//add the timer to the message
console.log($(options.message).find('.countDown'));
if($(options.message).find('.countDown').length > 0){
options.message = options.message + '(<strong class="countDown">'+options.timeOut+'</strong>s)';
}
}
options = $.extend({}, $.notify.defaults, options);
var div = $.tmpl($.notify.template, options),
notify = {
destroy : function() {
clearTimeout( notify.timer );
div.fadeOut(function() {
div.remove();
if(typeof options.onClose === 'function'){
options.onClose(this);
}
});
}
};
div.click( notify.destroy );
div.data( "notify", notify );
return div;
};
$.fn.notify = function(options) {
return this.map(function() {
var elem = $(this),
notify = $.notify(options).first(),
data = notify.data( "notify" );
elem.prepend(notify.hide());
//set a timeout to display the notification.
window.setTimeout(function(){
notify.fadeIn();
//automatically clear the timeout
if(options.timeOut){
data.updateTimeout = function() {
if(--options.timeOut) {
notify.find('.countDown').text(options.timeOut);
data.timer = setTimeout( data.updateTimeout, 1000 );
} else {
data.destroy();
}
};
data.timer = setTimeout( data.updateTimeout, 1000 );
}
}, options.delay);
return notify;
});
};
$.notify.template = '<div class="msg ${type}" style="display:none"><span>{{html message}}</span><a href="#" class="close">x</a></div>';
$.notify.defaults = {
type : "notice",
message : "This is a notice",
delay : 0, //time in ms to waitbefore displaying
timeOut: false, //how long until it fades out on it's own in seconds
onClose: false //on close callback
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment