Skip to content

Commit 0148499

Browse files
authored
fix(count-down): 修复标签页置于后台,倒计时停止问题 (#1631)
fix #1213
1 parent 5301977 commit 0148499

File tree

2 files changed

+43
-24
lines changed

2 files changed

+43
-24
lines changed

src/count-down/count-down.tsx

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { computed, defineComponent } from 'vue';
1+
import { computed, defineComponent, onBeforeUnmount, onMounted, ref } from 'vue';
22
import config from '../config';
33
import CountDownProps from './props';
44
import { useCountDown } from '../shared/useCountDown';
@@ -18,8 +18,17 @@ export default defineComponent({
1818
`${countDownClass.value}--${props.theme}`,
1919
`${countDownClass.value}--${props.size}`,
2020
]);
21-
22-
const { showTimes } = useCountDown(props);
21+
const visibility = ref(true);
22+
const visibilitychangeListener = () => {
23+
visibility.value = !document.hidden;
24+
};
25+
onMounted(() => {
26+
document.addEventListener('visibilitychange', visibilitychangeListener, false);
27+
});
28+
onBeforeUnmount(() => {
29+
document.removeEventListener('visibilitychange', visibilitychangeListener, false);
30+
});
31+
const { showTimes } = useCountDown(props, visibility);
2332
return () => {
2433
const renderContent = () => {
2534
const content = renderTNodeJSX('content');

src/shared/useCountDown/index.ts

+31-21
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { ref, reactive } from 'vue';
1+
import { ref, reactive, Ref, watch } from 'vue';
22
import { useRafFn } from '@vueuse/core';
33
import { TdUseCountDownProps, TdUseCountDown } from './type';
44
import { getRemainTimes, getShowTimes, getScreenFps } from './utils';
55
import { isBrowser } from '../util';
66

7-
export function useCountDown(props: TdUseCountDownProps): TdUseCountDown {
7+
export function useCountDown(props: TdUseCountDownProps, visibility?: Ref<boolean>): TdUseCountDown {
88
const {
99
time = 0,
1010
autoStart,
@@ -18,27 +18,37 @@ export function useCountDown(props: TdUseCountDownProps): TdUseCountDown {
1818
const fps = ref();
1919
const count = ref(Number(time));
2020
const showTimes = reactive(getShowTimes(getRemainTimes(time), format, millisecond, splitWithUnit));
21+
let hiddenTime = 0;
2122

22-
// raf
23-
const { pause, resume } = useRafFn(
24-
async () => {
25-
if (!isBrowser) return;
26-
if (!fps.value) {
27-
const res = await getScreenFps?.();
28-
fps.value = res || 60;
29-
}
30-
count.value = parseInt(`${Number(count.value) - 1000 / fps.value}`, 10);
31-
if (count.value <= 0) {
32-
pause?.();
33-
count.value = 0;
23+
visibility &&
24+
watch(visibility, (val) => {
25+
if (val) {
26+
count.value -= Date.now() - hiddenTime;
27+
rafFn();
28+
} else {
29+
hiddenTime = Date.now();
3430
}
35-
const times = getRemainTimes(count.value);
36-
onChange?.(times);
37-
count.value === 0 && onFinish?.();
38-
getShowTimes(times, format, millisecond, splitWithUnit)?.forEach?.((i, idx) => (showTimes[idx].value = i?.value));
39-
},
40-
{ immediate: autoStart },
41-
);
31+
});
32+
33+
const rafFn = async () => {
34+
if (!isBrowser) return;
35+
if (!fps.value) {
36+
const res = await getScreenFps?.();
37+
fps.value = res || 60;
38+
}
39+
count.value = parseInt(`${Number(count.value) - 1000 / fps.value}`, 10);
40+
if (count.value <= 0) {
41+
pause?.();
42+
count.value = 0;
43+
}
44+
const times = getRemainTimes(count.value);
45+
onChange?.(times);
46+
count.value === 0 && onFinish?.();
47+
getShowTimes(times, format, millisecond, splitWithUnit)?.forEach?.((i, idx) => (showTimes[idx].value = i?.value));
48+
};
49+
50+
// raf
51+
const { pause, resume } = useRafFn(rafFn, { immediate: autoStart });
4252

4353
/**
4454
* return

0 commit comments

Comments
 (0)