diff --git a/src/count-down/README.md b/src/count-down/README.md index 9ad60e550..64fef8755 100644 --- a/src/count-down/README.md +++ b/src/count-down/README.md @@ -134,26 +134,27 @@ export default { ### Events -| Event | Description | Arguments | -| --------------- | ---------------------------------- | -------------------- | -| finish | Triggered when count down finished | - | -| change `v2.4.4` | Triggered when count down changed | _timeData: TimeData_ | +| Event | Description | Arguments | +| --- | --- | --- | +| finish | Triggered when count down finished | - | +| change `v2.4.4` | Triggered when count down changed | _currentTime: CurrentTime_ | ### Slots -| Name | Description | SlotProps | -| ------- | -------------- | -------------------- | -| default | Custom Content | _timeData: TimeData_ | +| Name | Description | SlotProps | +| ------- | -------------- | -------------------------- | +| default | Custom Content | _currentTime: CurrentTime_ | ### TimeData Structure -| Name | Description | Type | -| ------------ | ------------------- | -------- | -| days | Remain days | _number_ | -| hours | Remain hours | _number_ | -| minutes | Remain minutes | _number_ | -| seconds | Remain seconds | _number_ | -| milliseconds | Remain milliseconds | _number_ | +| Name | Description | Type | +| ------------ | ----------------------------- | -------- | +| total | Total time, unit milliseconds | _number_ | +| days | Remain days | _number_ | +| hours | Remain hours | _number_ | +| minutes | Remain minutes | _number_ | +| seconds | Remain seconds | _number_ | +| milliseconds | Remain milliseconds | _number_ | ### Methods diff --git a/src/count-down/README.zh-CN.md b/src/count-down/README.zh-CN.md index 6f14effb6..7e3600bd6 100644 --- a/src/count-down/README.zh-CN.md +++ b/src/count-down/README.zh-CN.md @@ -144,26 +144,27 @@ export default { ### Events -| 事件名 | 说明 | 回调参数 | -| --------------- | ---------------- | -------------------- | -| finish | 倒计时结束时触发 | - | -| change `v2.4.4` | 倒计时变化时触发 | _timeData: TimeData_ | +| 事件名 | 说明 | 回调参数 | +| --------------- | ---------------- | -------------------------- | +| finish | 倒计时结束时触发 | - | +| change `v2.4.4` | 倒计时变化时触发 | _currentTime: CurrentTime_ | ### Slots -| 名称 | 说明 | SlotProps | -| ------- | ---------- | -------------------- | -| default | 自定义内容 | _timeData: TimeData_ | +| 名称 | 说明 | SlotProps | +| ------- | ---------- | -------------------------- | +| default | 自定义内容 | _currentTime: CurrentTime_ | -### TimeData 格式 +### CurrentTime 格式 -| 名称 | 说明 | 类型 | -| ------------ | -------- | -------- | -| days | 剩余天数 | _number_ | -| hours | 剩余小时 | _number_ | -| minutes | 剩余分钟 | _number_ | -| seconds | 剩余秒数 | _number_ | -| milliseconds | 剩余毫秒 | _number_ | +| 名称 | 说明 | 类型 | +| ------------ | ---------------------- | -------- | +| total | 剩余总时间(单位毫秒) | _number_ | +| days | 剩余天数 | _number_ | +| hours | 剩余小时 | _number_ | +| minutes | 剩余分钟 | _number_ | +| seconds | 剩余秒数 | _number_ | +| milliseconds | 剩余毫秒 | _number_ | ### 方法 diff --git a/src/count-down/index.js b/src/count-down/index.js index 33a135b1a..f571e5724 100644 --- a/src/count-down/index.js +++ b/src/count-down/index.js @@ -1,18 +1,12 @@ -import { - ref, - watch, - computed, - onActivated, - onDeactivated, - onBeforeUnmount, -} from 'vue'; +import { watch, computed } from 'vue'; // Utils -import { raf, cancelRaf, createNamespace } from '../utils'; -import { isSameSecond, parseTimeData, parseFormat } from './utils'; +import { createNamespace } from '../utils'; +import { parseFormat } from './utils'; // Composition import { useExpose } from '../composition/use-expose'; +import { useCountDown } from './use-count-down'; const [createComponent, bem] = createNamespace('count-down'); @@ -36,118 +30,37 @@ export default createComponent({ emits: ['change', 'finish'], setup(props, { emit, slots }) { - let rafId; - let endTime; - let counting; - let keepAlived; - - const remain = ref(0); - const timeData = computed(() => parseTimeData(remain.value)); - const timeText = computed(() => parseFormat(props.format, timeData.value)); - - const pause = () => { - counting = false; - cancelRaf(rafId); - }; - - const getCurrentRemain = () => Math.max(endTime - Date.now(), 0); - - const setRemain = (value) => { - remain.value = value; - emit('change', timeData.value); - - if (value === 0) { - pause(); + const { start, pause, reset, current } = useCountDown({ + time: +props.time, + millisecond: props.millisecond, + onChange(current) { + emit('change', current); + }, + onFinish() { emit('finish'); - } - }; + }, + }); - const microTick = () => { - rafId = raf(() => { - // in case of call reset immediately after finish - if (counting) { - setRemain(getCurrentRemain()); - - if (remain.value > 0) { - microTick(); - } - } - }); - }; - - const macroTick = () => { - rafId = raf(() => { - // in case of call reset immediately after finish - if (counting) { - const currentRemain = getCurrentRemain(); - - if ( - !isSameSecond(currentRemain, remain.value) || - currentRemain === 0 - ) { - setRemain(currentRemain); - } - - if (remain.value > 0) { - macroTick(); - } - } - }); - }; - - const tick = () => { - if (props.millisecond) { - microTick(); - } else { - macroTick(); - } - }; - - const start = () => { - if (!counting) { - endTime = Date.now() + remain.value; - counting = true; - tick(); - } - }; - - const reset = () => { - pause(); - remain.value = +props.time; + const timeText = computed(() => parseFormat(props.format, current.value)); + const resetTime = () => { + reset(+props.time); if (props.autoStart) { start(); } }; - watch(() => props.time, reset, { immediate: true }); - - onActivated(() => { - if (keepAlived) { - counting = true; - keepAlived = false; - tick(); - } - }); - - onDeactivated(() => { - if (counting) { - pause(); - keepAlived = true; - } - }); - - onBeforeUnmount(pause); + watch(() => props.time, resetTime, { immediate: true }); useExpose({ start, - reset, pause, + reset: resetTime, }); return () => (