Created
January 2, 2012 11:07
-
-
Save sj26/1550304 to your computer and use it in GitHub Desktop.
jQuery countdown counter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($) { | |
formatDuration = function(seconds) { | |
var duration = '', | |
days = Math.floor(seconds / 86400), | |
hours = Math.floor(seconds / 3600) % 24, | |
minutes = Math.floor(seconds / 60) % 60, | |
seconds = seconds % 60; | |
if (days) | |
duration = days + ' day' + (days > 1 ? 's' : '') + ', '; | |
duration += String("0" + hours).slice(-2) + ':' + String("0" + minutes).slice(-2) + ':' + String("0" + seconds).slice(-2); | |
return duration; | |
}; | |
$.fn.countdown = function() { | |
this.each(function() { | |
var self = $(this), | |
remain = self.data('countdown-remain'), | |
reached = self.data('countdown-reached') || '0', | |
since = new Date().getTime(), | |
countdown = function() { | |
var passed = Math.floor((new Date().getTime() - since) / 1000), | |
nowRemain = remain - passed; | |
self.text(nowRemain > 0 ? formatDuration(nowRemain) : reached); | |
}; | |
countdown(); | |
self.data('countdown-interval', setInterval(countdown, 1000)); | |
}); | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment