countdown.js
1.64 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
// 订单剩余时间显示及倒计时
require('../../common/bind-polyfill');
module.exports = {
count: null,
intervalTimer: null,
intervalValue: 60000,
$element: null,
selector: '.left-time',
addtionalMsg: '',
showSec: false,
setTime: function() {
var that = this;
this.$element.each(function(index, item) {
var $item = $(item);
var leftTime = $item.data('left');
var i = that.intervalValue / 1000;
$item.text(that.convertLeftTime(leftTime - that.count * i));
});
this.count += 1;
},
convertLeftTime: function(src) {
var sec = parseInt(src, 10) % 60;
var min = parseInt(src / 60, 10) % 60;
var hour = parseInt(src / 3600, 10);
var timeStr = min + '分';
if (src <= 0) {
timeStr = '已失效';
return timeStr;
}
if (this.showSec) {
timeStr += sec + '秒';
}
if (hour > 0) {
timeStr = hour + '时' + timeStr;
}
timeStr = '剩余' + timeStr;
if (this.addtionalMsg.length > 0) {
timeStr += this.addtionalMsg;
}
return timeStr;
},
getLeftTime: function() {
var that = this;
if (this.$element.length) {
this.setTime();
this.intervalTimer = setInterval(this.setTime.bind(that), that.intervalValue);
}
},
start: function() {
this.count = 0;
this.$element = $(this.selector);
if (this.intervalTimer) {
clearInterval(this.intervalTimer);
}
this.getLeftTime();
}
};