The code is as follows:
Copy Code code as follows:
/**
* Countdown
*
* @author Wadeyu
* @date 2012-04-20 17:40
* @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 ();
};
Examples are as follows:
Copy Code code as follows:
<body>
<div id= "Timecon" ></div>
<script type= "Text/javascript" >
var timecountdown = new Timecountdown (3 * 24 * 60 * 60);
Timecountdown.setshowcallback (function (obj) {//days, hours, minutes, seconds]
document.getElementById (' Timecon '). InnerHTML = Obj[0] + ":" + obj[1] + ":" + obj[2] + ': ' + obj[3];
});
Timecountdown.start ();
</script>
</body>