-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcount-down.vue
More file actions
182 lines (177 loc) · 3.93 KB
/
count-down.vue
File metadata and controls
182 lines (177 loc) · 3.93 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<template>
<span>
<slot v-bind="countdownData">{{countdownFormated}}</slot>
</span>
</template>
<script>
import {toMilliseconds, formatTime, toTimeData} from './util'
export default {
name: 'count-down',
props: {
/**
* remain days
*/
days: {
type: Number,
default: 0
},
/**
* remain hours
*/
hours: {
type: Number,
default: 0
},
/**
* remain minutes
*/
minutes: {
type: Number,
default: 0
},
/**
* remain seconds
*/
seconds: {
type: Number,
default: 0
},
/**
* whether autoplay or not
*/
autoplay: {
type: Boolean,
default: true
},
/**
* Output format.
* Default: 'dd 天 hh 时 mm 分 ss 秒'. These dd, hh, mm & ss specifiers are optional.
* The default value will change according to whether there are days, hours, minutes & seconds,
* e.g., if user just pass minutes, then the default value will be 'mm 分 ss 秒'
*
* 输出格式。
* 默认值:'dd 天 hh 时 mm 分 ss 秒'。dd、hh、mm和ss标识符都是可选的。
* 默认值会根据是否传入days, hours, minutes, seconds而变化,
* 比如用户只传了minutes,那么默认值就变为'mm 分 ss 秒'
*/
format: {
type: String,
default: ''
},
/**
* keep countdowm time in session
*/
keepInSession: {
type: String
}
},
data() {
return {
elapsed: 0,
rafId: null
}
},
computed: {
time() {
return toMilliseconds(this)
},
formatComputed() {
if (this.format) return this.format
const formatDefault = ['dd 天', 'hh 时', 'mm 分', 'ss 秒']
if (!this.days) {
formatDefault.shift()
if (!this.hours) {
formatDefault.shift()
if (!this.minutes) formatDefault.shift()
}
}
return formatDefault.join(' ')
},
countdown() {
return Math.max(0, this.time - this.elapsed)
},
countdownFormated() {
return formatTime(this.countdown, this.formatComputed)
},
countdownData() {
return toTimeData(this.countdown)
}
},
mounted() {
if (this.autoplay) this.start()
window.addEventListener('unload', this.setSession)
let session = sessionStorage.getItem(this.keepInSession)
if (session) {
session = JSON.parse(session)
const mountTime = session.rafId
? Number(new Date()) - session.unloadTime
: 0
this.elapsed = this.time - (session.countdown - mountTime)
}
},
beforeDestroy() {
cancelAnimationFrame(this.rafId)
this.setSession()
window.removeEventListener('unload', this.setSession)
},
methods: {
/**
* 开始计时
* @public
*/
start() {
if (this.rafId) return
let last
const step = () => {
this.rafId = requestAnimationFrame(timestamp => {
if (last) this.elapsed += Math.round(timestamp - last)
last = timestamp
if (this.countdown > 0) step()
else {
this.rafId = null
/**
* 计时结束事件
*/
this.clearSession()
this.$emit('finish')
}
})
}
step()
},
/**
* 暂停计时
* @public
*/
pause() {
cancelAnimationFrame(this.rafId)
this.rafId = null
},
/**
* 重置计时,需要调用start来再次启动
* @public
*/
reset() {
this.pause()
this.elapsed = 0
},
setSession() {
if (this.keepInSession) {
sessionStorage.setItem(
this.keepInSession,
JSON.stringify({
countdown: this.countdown,
unloadTime: Number(new Date()),
rafId: this.rafId
})
)
}
},
clearSession() {
sessionStorage.removeItem(this.keepInSession)
}
}
}
</script>
<style>
</style>