miniclock.js
2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* MiniClock Count Down
* Code in 2014/09/26
* By van
*/
define( 'miniclock' ,function( require, exports, module ){
var cfg = {
time: 60, //倒计数总数
interval: 100, //倒计时间隔
SD: "发送验证码",
WT: "秒后可重新发送",
isFormat: false, //格式化倒计数 {00:00:00}格式
atNow: false, //选择即时开始倒计时
mark: "s-waiting", //倒计时运行标记
ban: "bangrey" //倒计时禁用样式
},
miniclock = function( target,options ){
if ( options && typeof options == "object" ){
_assign( cfg,options );
}
cfg.cache = cfg.time;
if( $( target ).hasClass( cfg.mark ) ){
return;
}else{
$( target ).addClass( cfg.mark+" "+cfg.ban );
_countDown( target );
};
};
miniclock.prototype = {
countDown: _countDown
}
function _countDown( tag ){
var format,time=cfg.time,clock;
$( tag ).addClass( cfg.mark )
var clock = _recyle( function(){
if ( time==0 ){
$( tag ).html( cfg.SD ).removeClass( cfg.mark+" "+cfg.ban );
clearInterval( clock );
return false;
}
time -= 1;
$( tag ).html( time+cfg.WT );
} );
};
function _formatCountDown(){
var format;
var clock = _recyle( function(){
if ( cfg.time==0 ){
cfg.time = cfg.cache;
tag.innerHTML = cfg.SD;
tag.removeAttribute( "class" );
clearInterval( clock );
return false;
}
cfg.time -= 1;
format = _format(cfg.time);
tag.innerHTML = format+cfg.WT;
} );
};
function _recyle( func ){
return setInterval( func,cfg.interval );
};
function _assign( a,b ){
if (typeof b != "object"){return false};
for (var i in b){
a[i] = b[i];
};
return null;
}
function _format( n ){
var h,m,s;
h = parseInt(n/(60*60));
m = parseInt(n/60);
s = n%60;
return (
(h<10 ? "0"+h : h)+
":"+ ( m<10 ? "0"+m : m )+
":"+ s
) ;
};
return miniclock;
});