index.js
2.23 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
90
let timer;
Component({
properties: {
type: String,
current: {
type: Number,
observer: '_dataChange'
},
begin: Number,
end: Number
},
data: {
label: '',
days: '',
hours: '',
minutes: '',
seconds: '',
diff: '',
fullContent: ''
},
pageLifetimes: {
show() {
console.log('show');
},
hide() {
console.log('hide');
clearInterval(timer);
}
},
methods: {
_dataChange() {
const {current, begin, end} = this.data;
let label = '距开售';
// if (current && begin && end) {
// label = begin - current > 0 ? '距离活动开始' : '距离活动结束';
// }
this.data.diff = Math.abs(begin - current > 0 ? begin - current : end - current);
if (timer) {
clearInterval(timer);
}
this.tick(label);
timer = setInterval(() => {
this.tick(label);
--this.data.diff;
if (this.data.diff < 0) {
clearInterval(timer);
this.triggerEvent('endcountdown', {
isEnd: current - begin > 0
});
}
}, 1000);
},
tick(label) {
let diff = this.data.diff;
let days = Math.floor(diff / (24 * 3600));
days = days > 9 ? days : '0' + days;
let leave1 = diff % (24 * 3600);
let hours = Math.floor(leave1 / (3600));
hours = hours > 9 ? hours : '0' + hours;
let leave2 = leave1 % 3600;
let minutes = Math.floor(leave2 / 60);
minutes = minutes > 9 ? minutes : '0' + minutes;
let seconds = leave2 % 60;
seconds = seconds > 9 ? seconds : '0' + seconds;
// let fullContent = label + ' ' + days + ' 天 ' + hours + ':' + minutes + ':' + seconds;
let fullContent = days + ' :' + hours + ' :' + minutes;
this.setData({
label,
days,
hours,
minutes,
seconds,
fullContent
});
}
}
});