countdown.js
745 Bytes
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
/**
* 倒计时 mm:ss
*
* @author: liqi <qi.li@yoho.cn>
* @date: 2016/11/11
*/
/**
* 格式化
* @param total 总的分钟数
* @param el 倒计时显示元素
* @returns {string}
*/
function _secsFormat(total, el) {
var mins = Math.floor(total / 60);
var secs = total - mins * 60;
mins < 10 && (mins = '0' + mins);
secs < 10 && (secs = '0' + secs);
el && el.html(mins + ':' + secs);
if (!el) {
return mins + ':' + secs;
}
}
function countdown(mins, el) {
var interval,
secs = mins * 60;
interval = setInterval(function() {
_secsFormat(--secs, el);
secs === 0 && clearInterval(interval);
}, 1000);
_secsFormat(secs, el);
}
module.exports = countdown;