/** * @name jquery countdown plugin * @author martin angelov * @version 1.0 * @url http://tutorialzine.com/2011/12/countdown-jquery/ * @license mit license */ (function($){ // number of seconds in every time division var days = 24*60*60, hours = 60*60, minutes = 60; // creating the plugin $.fn.countdown = function(prop){ var options = $.extend({ callback : function(){}, timestamp : 0, servertime : 0, el : null },prop); var left, d, h, m, s, positions; // initialize the plugin init(this, options); positions = this.find('.position'); (function tick(){ _addtime = options.el.attr('data-add-time')*1; // time left left = _addtime + math.floor((options.timestamp - (options.servertime)) / 1000); options.el.attr('data-add-time', _addtime - 1); if(left < 0){ left = 0; } // number of days left d = math.floor(left / days); updateduo(0, 1, d); left -= d*days; // number of hours left h = math.floor(left / hours); updateduo(2, 3, h); left -= h*hours; // number of minutes left m = math.floor(left / minutes); updateduo(4, 5, m); left -= m*minutes; // number of seconds left s = left; updateduo(6, 7, s); // calling an optional user supplied callback options.callback(d, h, m, s); // scheduling another call of this function in 1s settimeout(tick, 1000); })(); // this function updates two digit positions at once function updateduo(minor,major,value){ switchdigit(positions.eq(minor),math.floor(value/10)%10); switchdigit(positions.eq(major),value%10); } return this; }; function init(elem, options){ elem.addclass('countdownholder'); // creating the markup inside the container $.each(['days','hours','minutes','seconds'],function(i){ $('').html( '\ 0\ \ \ 0\ ' ).appendto(elem); if(this!="seconds"){ elem.append(''); } if(this=="days"){ $(elem).find('.countdays').append('days'); } if(this=="hours"){ $(elem).find('.counthours').append('hours'); } if(this=="minutes"){ $(elem).find('.countminutes').append('minutes'); } if(this=="seconds"){ $(elem).find('.countseconds').append('seconds'); } }); } // creates an animated transition between the two numbers function switchdigit(position,number){ var digit = position.find('.digit') if(digit.is(':animated')){ return false; } if(position.data('digit') == number){ // we are already showing this number return false; } position.data('digit', number); var replacement = $('',{ 'class':'digit', css:{ top:'-23px', opacity:0 }, html:number }); // the .static class is added when the animation // completes. this makes it run smoother. digit .before(replacement) .removeclass('static') .animate({top:'23px',opacity:0},'fast',function(){ digit.remove(); }) replacement .delay(100) .animate({top:0,opacity:1},'fast',function(){ replacement.addclass('static'); }); } })(jquery);