-
Notifications
You must be signed in to change notification settings - Fork 5
feat: 添加刷新倒计时记录不丢失 #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| ```vue | ||
| <template> | ||
| <div> | ||
| <count-down :seconds="59" :hours="1" keep-in-session="my_keep" ref="cd" /> | ||
| <button @click="onClick('start')">start</button> | ||
| <button @click="onClick('pause')">pause</button> | ||
| <button @click="onClick('reset')">reset</button> | ||
| </div> | ||
| </template> | ||
| <script> | ||
| export default { | ||
| methods: { | ||
| onClick(m) { | ||
| this.$refs.cd[m]() | ||
| } | ||
| }, | ||
| } | ||
| </script> | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,6 +59,13 @@ export default { | |
| format: { | ||
| type: String, | ||
| default: '' | ||
| }, | ||
|
|
||
| /** | ||
| * keep countdowm time in session | ||
| */ | ||
| keepInSession: { | ||
| type: String | ||
| } | ||
| }, | ||
| data() { | ||
|
|
@@ -96,6 +103,20 @@ export default { | |
| }, | ||
| 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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 直接记startTime不就行了?不用又记unloadTime又记countdown吧 |
||
| } | ||
| }, | ||
| beforeDestroy() { | ||
| cancelAnimationFrame(this.rafId) | ||
| this.setSession() | ||
| window.removeEventListener('unload', this.setSession) | ||
|
||
| }, | ||
| methods: { | ||
| /** | ||
|
|
@@ -115,6 +136,7 @@ export default { | |
| /** | ||
| * 计时结束事件 | ||
| */ | ||
| this.clearSession() | ||
| this.$emit('finish') | ||
| } | ||
| }) | ||
|
|
@@ -136,6 +158,21 @@ export default { | |
| reset() { | ||
| this.pause() | ||
| this.elapsed = 0 | ||
| }, | ||
| setSession() { | ||
| if (this.keepInSession) { | ||
| sessionStorage.setItem( | ||
| this.keepInSession, | ||
| JSON.stringify({ | ||
| countdown: this.countdown, | ||
| unloadTime: Number(new Date()), | ||
eviIIt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| rafId: this.rafId | ||
| }) | ||
| ) | ||
| } | ||
| }, | ||
| clearSession() { | ||
| sessionStorage.removeItem(this.keepInSession) | ||
| } | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.