The Code is as follows:
1 /**
2 * countdown
3 *
4 * @ author WadeYu
5 * @ date
6 * @ copyright boyaa.com
7 */
8 var TimeCountDown = function (initTime ){
9 var day = 0;
10 var hour = 0;
11 var minute = 0;
12 var second = 0;
13 var timerId = 0;
14 var self = this;
15 var fixInitTime = function (){
16 initTime = parseInt (initTime );
17 if (isNaN (initTime) | initTime <0 ){
18 initTime = 0;
19}
20 };
21 var fixZero = function (num ){
22 return num <10? ('0' + num): num;
23 };
24 var calc = function (){
25 showCallback. call (self, [fixZero (day), fixZero (hour), fixZero (minute), fixZero (second)]);
26 if (day = 0 & hour = 0 & minute = 0 & second = 0 ){
27 self. stop ();
28} else {
29 if (hour ===0 & day> 0 ){
30 hour = 24;
31 day --;
32}
33 if (minute = 0 & hour> 0 ){
34 minute = 60;
35 hour --;
36}
37 if (second = 0 & minute> 0 ){
38 second = 59;
39 minute --;
40} else {
41 second --;
42}
43 timerId = window. setTimeout (function (){
44 calc ();
45}, 1000 );
46}
47 };
48 var showCallback = function (){
49 alert ('Please set show callback functions! ');
50 };
51 var init = function (){
52 fixInitTime ();
53 if (initTime> 0 ){
54 day = Math. floor (initTime/(24*60*60 )));
55 initTime-= day * 24*60*60;
56 hour = Math. floor (initTime/(60*60 ));
57 initTime-= hour * 60*60;
58 minute = Math. floor (initTime/60 );
59 second = initTime-minute * 60;
60}
61 };
62 this. start = function (){
63 calc ();
64 };
65 this. stop = function (){
66 timerId & window. clearTimeout (timerId );
67 };
68 this. setShowCallback = function (fn ){
69 typeof (fn) = 'function '? (ShowCallback = fn ):'';
70 };
71 init ();
72 };
Example:
<Body>
<Div id = "timeCon"> </div>
<Script type = "text/javascript">
Var timeCountDown = new TimeCountDown (3*24*60*60 );
TimeCountDown. setShowCallback (function (obj) {// [day, hour, minute, second]
Document. getElementById ('timecon '). innerHTML = obj [0] + ":" + obj [1] + ":" + obj [2] + ':' + obj [3];
});
TimeCountDown. start ();
</Script>
</Body>
From huan & ping