count-down.vue
1.24 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
<template>
<div class="count-down-wrapper">
<span></span>
</div>
</template>
<script>
export default {
props: {
leftTime: {
type: Number,
default: 0
}
},
data() {
return {
remainTime: this.props.leftTime,
countDown: "",
timeoutId: null
};
},
mounted() {
this.countDown = this.formatTime();
this.timeoutId = setInterval(() => {
this.remainTime--;
this.countDown = this.formatTime();
}, 1000);
},
destroyed() {
clearInterval(this.timeoutId);
},
computed: {
timeList: function() {}
},
methods: {
formatTime() {
if (this.remainTime < 0) {
return ["00", "00", "00"];
}
let remainTime = this.remainTime;
const hourInSecond = 60 * 60;
const numberOfHours = Math.floor(this.remainTime / hourInSecond);
remainTime = remainTime - numberOfHours * hourInSecond;
const numberOfMinutes = Math.floor(remainTime / 60);
const numberOfSeconds = remainTime - numberOfMinutes * 60;
return [numberOfHour, numberOfMinute, numberOfSeconds].map(time => {
return time < 10 ? `0${time}` : `${time}`;
});
}
}
};
</script>
<style lang="scss" scoped>
.count-down-wrapper {
display: flex;
}
</style>