Skip to content

Instantly share code, notes, and snippets.

@psdcoder
Last active December 15, 2015 15:39
Show Gist options
  • Save psdcoder/5283719 to your computer and use it in GitHub Desktop.
Save psdcoder/5283719 to your computer and use it in GitHub Desktop.
Simple jQuery timer. Usage: $('#timer').timer( 10, //duration of timer function(el){ //callback which will executed when time is up el.text('time is up!'); } )
(function($){
$.fn.timer = function(duration, callback) {
var time = duration,
element = this;
element.text(time--);
var interval = setInterval(function() {
if(time < 0){
clearInterval(interval);
if(typeof callback === 'function'){
callback(element);
}
}else{
element.text(time);
time--;
}
}, 1000);
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment