fix(CountDown): infinite tick if call reset after finish (#5340)

This commit is contained in:
neverland 2019-12-20 20:23:56 +08:00 committed by GitHub
parent 07899dbe12
commit 8cb653afc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 25 deletions

View File

@ -79,7 +79,7 @@ export default {
:time="3000"
:auto-start="false"
format="ss:SSS"
@finish="finished"
@finish="finish"
/>
<van-grid clickable :column-num="3">
<van-grid-item text="Start" icon="play-circle-o" @click="start" />

View File

@ -89,7 +89,7 @@ export default {
:time="3000"
:auto-start="false"
format="ss:SSS"
@finish="finished"
@finish="finish"
/>
<van-grid clickable>
<van-grid-item text="开始" icon="play-circle-o" @click="start" />

View File

@ -5,24 +5,15 @@
</demo-block>
<demo-block :title="$t('customFormat')">
<van-count-down
:time="time"
:format="$t('formatWithDay')"
/>
<van-count-down :time="time" :format="$t('formatWithDay')" />
</demo-block>
<demo-block :title="$t('millisecond')">
<van-count-down
millisecond
:time="time"
format="HH:mm:ss:SS"
/>
<van-count-down millisecond :time="time" format="HH:mm:ss:SS" />
</demo-block>
<demo-block :title="$t('customStyle')">
<van-count-down
:time="time"
>
<van-count-down :time="time">
<template v-slot="currentTime">
<span class="item">{{ currentTime.hours }}</span>
<span class="item">{{ currentTime.minutes }}</span>
@ -40,10 +31,7 @@
format="ss:SSS"
@finish="$toast($t('finished'))"
/>
<van-grid
clickable
:column-num="3"
>
<van-grid clickable :column-num="3">
<van-grid-item
icon="play-circle-o"
:text="$t('start')"
@ -54,11 +42,7 @@
:text="$t('pause')"
@click="pause"
/>
<van-grid-item
icon="replay"
:text="$t('reset')"
@click="reset"
/>
<van-grid-item icon="replay" :text="$t('reset')" @click="reset" />
</van-grid>
</demo-block>
</demo-section>

View File

@ -64,6 +64,7 @@ export default createComponent({
},
methods: {
// @exposed-api
start() {
if (this.counting) {
return;
@ -74,11 +75,13 @@ export default createComponent({
this.tick();
},
// @exposed-api
pause() {
this.counting = false;
cancelRaf(this.rafId);
},
// @exposed-api
reset() {
this.pause();
this.remain = this.time;
@ -98,9 +101,15 @@ export default createComponent({
microTick() {
this.rafId = raf(() => {
/* istanbul ignore if */
// in case of call reset immediately after finish
if (!this.counting) {
return;
}
this.setRemain(this.getRemain());
if (this.remain !== 0) {
if (this.remain > 0) {
this.microTick();
}
});
@ -108,13 +117,19 @@ export default createComponent({
macroTick() {
this.rafId = raf(() => {
/* istanbul ignore if */
// in case of call reset immediately after finish
if (!this.counting) {
return;
}
const remain = this.getRemain();
if (!isSameSecond(remain, this.remain) || remain === 0) {
this.setRemain(remain);
}
if (this.remain !== 0) {
if (this.remain > 0) {
this.macroTick();
}
});