forked from getanwar/vuejs-countdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Countdown.vue
123 lines (115 loc) · 2.51 KB
/
Countdown.vue
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<template>
<ul class="vue-countdown">
<li>
<p class="digit">{{ days | twoDigits }}</p>
<p class="text">days</p>
</li>
<li>
<p class="digit">{{ hours | twoDigits }}</p>
<p class="text">hours</p>
</li>
<li>
<p class="digit">{{ minutes | twoDigits }}</p>
<p class="text">Min</p>
</li>
<li>
<p class="digit">{{ seconds | twoDigits }}</p>
<p class="text">Sec</p>
</li>
</ul>
</template>
<script>
import Vue from 'vue'
Vue.filter('twoDigits', (value) => {
if ( value.toString().length <= 1 ) {
return '0'+value.toString()
}
return value.toString()
})
let interval = null;
export default {
props: ['deadline', 'stop'],
data() {
return {
now: Math.trunc((new Date()).getTime() / 1000),
diff: 0
}
},
mounted() {
interval = setInterval(() => {
this.now = Math.trunc((new Date()).getTime() / 1000)
}, 1000)
console.log(interval)
},
computed: {
date() {
return Math.trunc(Date.parse(this.deadline.replace(/-/g, "/")) / 1000)
},
seconds() {
return Math.trunc(this.diff) % 60
},
minutes() {
return Math.trunc(this.diff / 60) % 60
},
hours() {
return Math.trunc(this.diff / 60 / 60) % 24
},
days() {
return Math.trunc(this.diff / 60 / 60 / 24)
}
},
watch: {
now(value){
this.diff = this.date - this.now;
if(this.diff <= 0 || this.stop){
this.diff = 0;
// Remove interval
clearInterval(interval);
this.$emit('stop')
}
}
}
}
</script>
<style>
.vue-countdown {
padding: 0;
margin: 0;
}
.vue-countdown li {
display: inline-block;
margin: 0 8px;
text-align: center;
position: relative;
}
.vue-countdown li p {
margin: 0;
}
.vue-countdown li:after {
content: ":";
position: absolute;
top: 30px;
right: -13px;
font-size: 32px;
}
.vue-countdown li:first-of-type {
margin-left: 0;
}
.vue-countdown li:last-of-type {
margin-right: 0;
}
.vue-countdown li:last-of-type:after {
content: "";
}
.vue-countdown .digit {
font-size: 82px;
font-weight: 600;
line-height: 1.4;
margin-bottom: 0;
}
.vue-countdown .text {
text-transform: uppercase;
margin-bottom: 0;
font-size: 15px;
}
</style>