-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.jCountr.min.js
11 lines (10 loc) · 3.56 KB
/
jquery.jCountr.min.js
1
2
3
4
5
6
7
8
9
10
11
/**
* jCountr
* Creates a countdown timer from a jQuery object.
*
* $(<selector>).counter({params});
*
* @author agentor@gmx.de
* @version 1.2 [2009-05-06]
*/
jQuery.fn.counter=function(params){var self=this;return self.display=$(this),self.hour=params.hour?params.hour:"0",self.min=params.min?params.min:"0",self.sec=params.sec?params.sec:"0",self.hour_end=params.hour_end?params.hour_end:"0",self.min_end=params.min_end?params.min_end:"0",self.sec_end=params.sec_end?params.sec_end:"0",self.message=params.message?params.message:"FINISHED",self.delimiter=params.delimiter?params.delimiter:":",self.return_url=params.return_url?params.return_url:"",self.finish_function=params.finish_function?params.finish_function:"",self.interval=params.interval?1e3*params.interval:"1000",self.iteration=params.iteration?params.iteration:"1",self.direction=params.direction?params.direction:"down",self.timestamp=60*self.hour*60+60*self.min+1*self.sec,self.timestamp_end=60*self.hour_end*60+60*self.min_end+1*self.sec_end,self.onTick=params.onTick?params.onTick:function(){return!0},self.onFinish=params.onFinish?params.onFinish:function(){return!0},self._updateCounter=function(e){if(self.change_hour=e.change_hour?e.change_hour:"0",self.change_min=e.change_min?e.change_min:"0",self.change_sec=e.change_sec?e.change_sec:"0",self.change_direction=e.change_direction?e.change_direction:"up",self.hour=e.hour?e.hour:self.hour,self.min=e.min?e.min:self.min,self.sec=e.sec?e.sec:self.sec,self.hour_end=e.hour_end?e.hour_end:self.hour_end,self.min_end=e.min_end?e.min_end:self.min_end,self.sec_end=e.sec_end?e.sec_end:self.sec_end,self.message=e.message?e.message:self.message,self.delimiter=e.delimiter?e.delimiter:self.delimiter,self.return_url=e.return_url?e.return_url:self.return_url,self.finish_function=e.finish_function?e.finish_function:self.finish_function,self.interval=e.interval?1e3*e.interval:self.interval,self.iteration=e.iteration?e.iteration:self.iteration,self.direction=e.direction?e.direction:self.direction,self.timestamp_end=60*self.hour_end*60+60*self.min_end+1*self.sec_end,0!=self.change_hour||0!=self.change_min||0!=self.change_sec){var s=self._createTimestamp(self.change_hour,self.change_min,self.change_sec);if(self.change_direction.match(/up/))self.timestamp+=s;else{if(!self.change_direction.match(/down/))return;self.timestamp-=s}}else self.timestamp=60*self.hour*60+60*self.min+1*self.sec},self._createTimestamp=function(e,s,n){return 60*e*60+60*s+1*n},self._getCounterFromTimestamp=function(e){return e>0?(hours=Math.floor(e/3600),minutes=Math.floor(60*(e/3600-hours)),seconds=Math.round(60*(60*(e/3600-hours)-minutes))):(hours=0,minutes=0,seconds=0),60==seconds&&(seconds=0),0==seconds&&0!=hours&&(minutes=minutes/1+1),minutes<10&&(minutes<0&&(minutes=0),minutes="0"+minutes),seconds<10&&(seconds<0&&(seconds=0),seconds="0"+seconds),hours<10&&(hours<0&&(hours=0),hours="0"+hours),hours>0?hours+self.delimiter+minutes+self.delimiter+seconds:minutes+self.delimiter+seconds},self._tick=function(){if(!(self.timestamp>0&&self.timestamp!=self.timestamp_end)){if(window.clearInterval(self._interval),!self.onFinish(self.display))return;return self.display.html(self.message),""!=self.finish_function&&eval(self.finish_function),void(""!=self.return_url&&(window.location.href=self.return_url))}var counter=self._getCounterFromTimestamp(self.timestamp);if(self.display.html(counter),"down"==self.direction)self.timestamp=1*self.timestamp-1*self.iteration;else{if("up"!=self.direction)return;self.timestamp=1*self.timestamp+1*self.iteration}},self._tick(),self._interval=window.setInterval(self._tick,self.interval),this};