The javascript countdown function implements the Code. For more information, see the following code:
The Code is as follows:
/**
* Countdown
*
* @ Author WadeYu
* @ Date
* @ Copyright boyaa.com
*/
Var TimeCountDown = function (initTime ){
Var day = 0;
Var hour = 0;
Var minute = 0;
Var second = 0;
Var timerId = 0;
Var self = this;
Var fixInitTime = function (){
InitTime = parseInt (initTime );
If (isNaN (initTime) | initTime <0 ){
InitTime = 0;
}
};
Var fixZero = function (num ){
Return num <10? ('0' + num): num;
};
Var calc = function (){
ShowCallback. call (self, [fixZero (day), fixZero (hour), fixZero (minute), fixZero (second)]);
If (day = 0 & hour = 0 & minute = 0 & second = 0 ){
Self. stop ();
} Else {
If (hour === 0 & day> 0 ){
Hour = 24;
Day --;
}
If (minute = 0 & hour> 0 ){
Minute = 60;
Hour --;
}
If (second = 0 & minute> 0 ){
Second = 59;
Minute --;
} Else {
Second --;
}
TimerId = window. setTimeout (function (){
Calc ();
},1000 );
}
};
Var showCallback = function (){
Alert ('Please set show callback functions! ');
};
Var init = function (){
FixInitTime ();
If (initTime> 0 ){
Day = Math. floor (initTime/(24*60*60 )));
InitTime-= day * 24*60*60;
Hour = Math. floor (initTime/(60*60 ));
InitTime-= hour * 60*60;
Minute = Math. floor (initTime/60 );
Second = initTime-minute * 60;
}
};
This. start = function (){
Calc ();
};
This. stop = function (){
TimerId & window. clearTimeout (timerId );
};
This. setShowCallback = function (fn ){
Typeof (fn) = 'function '? (ShowCallback = fn ):'';
};
Init ();
};
Example:
The Code is as follows: