Javascript countdown implementation code _ time and date

Source: Internet
Author: User
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:




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.