Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #4 from eusonlito/master

Fixed stop when countdown come to 0 and added continuous countdown.
  • Loading branch information...
commit ad96daa80195a8e21ce7608b009157ce050e7988 2 parents 0f772e2 + 719e738
@Reflejo authored
Showing with 49 additions and 21 deletions.
  1. +12 −0 README.md
  2. +32 −19 js/jquery.countdown.js
  3. +5 −2 js/jquery.countdown.min.js
View
12 README.md
@@ -23,6 +23,18 @@ This project is still at beta stage, and probably it will remain that way :(. No
});
```
+### Added continuously countdown
+
+```javascript
+ $('#counter').countdown({
+ format: 'sss',
+ startTime: "120",
+ continuous: true,
+ timerEnd: function() { alert('end!!'); },
+ image: "digits.png"
+ });
+```
+
Did I mention that js code weighs just **1.7 KB**?
### Author
View
51 js/jquery.countdown.js
@@ -4,6 +4,9 @@
* Copyright (c) 2009 Martin Conte Mac Donell <Reflejo@gmail.com>
* Dual licensed under the MIT and GPL licenses.
* http://docs.jquery.com/License
+ *
+ * Forked by Lito in Github
+ * https://github.com/eusonlito/jquery-countdown
*/
jQuery.fn.countdown = function(userOptions)
@@ -19,7 +22,8 @@ jQuery.fn.countdown = function(userOptions)
digitWidth: 67,
digitHeight: 90,
timerEnd: function(){},
- image: "digits.png"
+ image: "digits.png",
+ continuous: false
};
var digits = [], intervals = [];
@@ -45,26 +49,34 @@ jQuery.fn.countdown = function(userOptions)
margin(c, -elem.current * options.digitHeight * options.digitImages);
- // Add max digits, for example, first digit of minutes (mm) has
- // a max of 5. Conditional max is used when the left digit has reach
- // the max. For example second "hours" digit has a conditional max of 4
- switch (options.format[i])
+ if (options.continuous === true)
+ {
+ digits[c]._max = function(){ return 9; };
+ }
+ else
{
- case 'h':
- digits[c]._max = function(pos, isStart) {
- if (pos % 2 == 0)
- return 2;
- else
- return (isStart) ? 3: 9;
- };
- break;
- case 'd':
- digits[c]._max = function(){ return 9; };
- break;
- case 'm':
- case 's':
- digits[c]._max = function(pos){ return (pos % 2 == 0) ? 5: 9; };
+ // Add max digits, for example, first digit of minutes (mm) has
+ // a max of 5. Conditional max is used when the left digit has reach
+ // the max. For example second "hours" digit has a conditional max of 4
+ switch (options.format[i])
+ {
+ case 'h':
+ digits[c]._max = function(pos, isStart) {
+ if (pos % 2 == 0)
+ return 2;
+ else
+ return (isStart) ? 3: 9;
+ };
+ break;
+ case 'd':
+ digits[c]._max = function(){ return 9; };
+ break;
+ case 'm':
+ case 's':
+ digits[c]._max = function(pos){ return (pos % 2 == 0) ? 5: 9; };
+ }
}
+
++c;
}
else
@@ -134,6 +146,7 @@ jQuery.fn.countdown = function(userOptions)
for (var i = 0; i < digits.length; i++)
{
clearInterval(intervals[i]);
+ clearInterval(intervals.main);
margin(i, 0);
}
options.timerEnd();
View
7 js/jquery.countdown.min.js
@@ -1,8 +1,11 @@
/*
- * jquery-counter plugin
+ * jquery-countdown plugin
*
* Copyright (c) 2009 Martin Conte Mac Donell <Reflejo@gmail.com>
* Dual licensed under the MIT and GPL licenses.
* http://docs.jquery.com/License
+ *
+ * Forked by Lito in Github
+ * https://github.com/eusonlito/jquery-countdown
*/
-jQuery.fn.countdown=function(a){var b={stepTime:60,format:"dd:hh:mm:ss",startTime:"01:12:32:55",digitImages:6,digitWidth:67,digitHeight:90,timerEnd:function(){},image:"digits.png"};var c=[],d=[];var e=function(a){var d=0;for(var e=0;e<b.startTime.length;e++){if(parseInt(b.startTime[e])>=0){elem=$('<div id="cnt_'+d+'" class="cntDigit" />').css({height:b.digitHeight,"float":"left",background:"url('"+b.image+"')",width:b.digitWidth});elem.current=parseInt(b.startTime[e]);c.push(elem);f(d,-elem.current*b.digitHeight*b.digitImages);switch(b.format[e]){case"h":c[d]._max=function(a,b){if(a%2==0)return 2;else return b?3:9};break;case"d":c[d]._max=function(){return 9};break;case"m":case"s":c[d]._max=function(a){return a%2==0?5:9}}++d}else{elem=$('<div class="cntSeparator"/>').css({"float":"left"}).text(b.startTime[e])}a.append(elem)}};var f=function(a,b){if(b!==undefined){c[a].margin=b;return c[a].css({backgroundPosition:"0 "+b+"px"})}return c[a].margin||0};var g=function(a,e,g){if(d[a])window.clearInterval(d[a]);var h=-(b.digitHeight*b.digitImages*c[a].current);f(a,h);c[a].current=c[a].current+(g?e:-e);var i=0;d[a]=setInterval(function(){if(i++===b.digitImages*e){window.clearInterval(d[a]);delete d[a];return}var c=g?-b.digitHeight:b.digitHeight;f(a,h+i*c)},b.stepTime/e)};var h=function(a){if(c[a].current==0){if(a>0){var e=c[a-1].current==0;g(a,c[a]._max(a,e),true);h(a-1)}else{for(var i=0;i<c.length;i++){clearInterval(d[i]);f(i,0)}b.timerEnd()}return}g(a,1)};$.extend(b,a);e(this);d.main=setInterval(function(){h(c.length-1)},1e3)}
+jQuery.fn.countdown=function(userOptions){var options={stepTime:60,format:"dd:hh:mm:ss",startTime:"01:12:32:55",digitImages:6,digitWidth:67,digitHeight:90,timerEnd:function(){},image:"digits.png",continuous:false};var digits=[],intervals=[];var createDigits=function(where){var c=0;for(var i=0;i<options.startTime.length;i++){if(parseInt(options.startTime[i])>=0){elem=$('<div id="cnt_'+c+'" class="cntDigit" />').css({height:options.digitHeight,float:'left',background:'url(\''+options.image+'\')',width:options.digitWidth});elem.current=parseInt(options.startTime[i]);digits.push(elem);margin(c,-elem.current*options.digitHeight*options.digitImages);if(options.continuous===true){digits[c]._max=function(){return 9}}else{switch(options.format[i]){case'h':digits[c]._max=function(pos,isStart){if(pos%2==0)return 2;else return(isStart)?3:9};break;case'd':digits[c]._max=function(){return 9};break;case'm':case's':digits[c]._max=function(pos){return(pos%2==0)?5:9}}}++c}else{elem=$('<div class="cntSeparator"/>').css({float:'left'}).text(options.startTime[i])}where.append(elem)}};var margin=function(elem,val){if(val!==undefined){digits[elem].margin=val;return digits[elem].css({'backgroundPosition':'0 '+val+'px'})}return digits[elem].margin||0};var makeMovement=function(elem,steps,isForward){if(intervals[elem])window.clearInterval(intervals[elem]);var initialPos=-(options.digitHeight*options.digitImages*digits[elem].current);margin(elem,initialPos);digits[elem].current=digits[elem].current+((isForward)?steps:-steps);var x=0;intervals[elem]=setInterval(function(){if(x++===options.digitImages*steps){window.clearInterval(intervals[elem]);delete intervals[elem];return}var diff=isForward?-options.digitHeight:options.digitHeight;margin(elem,initialPos+(x*diff))},options.stepTime/steps)};var moveDigit=function(elem){if(digits[elem].current==0){if(elem>0){var isStart=(digits[elem-1].current==0);makeMovement(elem,digits[elem]._max(elem,isStart),true);moveDigit(elem-1)}else{for(var i=0;i<digits.length;i++){clearInterval(intervals[i]);clearInterval(intervals.main);margin(i,0)}options.timerEnd()}return}makeMovement(elem,1)};$.extend(options,userOptions);createDigits(this);intervals.main=setInterval(function(){moveDigit(digits.length-1)},1000)};
Please sign in to comment.
Something went wrong with that request. Please try again.