Skip to content
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

fix(count-down): 修复标签页置于后台,倒计时停止问题 #1631

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/count-down/count-down.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { computed, defineComponent } from 'vue';
import { computed, defineComponent, onBeforeUnmount, onMounted, ref } from 'vue';
import config from '../config';
import CountDownProps from './props';
import { useCountDown } from '../shared/useCountDown';
Expand All @@ -18,8 +18,17 @@ export default defineComponent({
`${countDownClass.value}--${props.theme}`,
`${countDownClass.value}--${props.size}`,
]);

const { showTimes } = useCountDown(props);
const visibility = ref(true);
const visibilitychangeListener = () => {
visibility.value = !document.hidden;
};
onMounted(() => {
document.addEventListener('visibilitychange', visibilitychangeListener, false);
});
onBeforeUnmount(() => {
document.removeEventListener('visibilitychange', visibilitychangeListener, false);
});
const { showTimes } = useCountDown(props, visibility);
return () => {
const renderContent = () => {
const content = renderTNodeJSX('content');
Expand Down
52 changes: 31 additions & 21 deletions src/shared/useCountDown/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ref, reactive } from 'vue';
import { ref, reactive, Ref, watch } from 'vue';
import { useRafFn } from '@vueuse/core';
import { TdUseCountDownProps, TdUseCountDown } from './type';
import { getRemainTimes, getShowTimes, getScreenFps } from './utils';
import { isBrowser } from '../util';

export function useCountDown(props: TdUseCountDownProps): TdUseCountDown {
export function useCountDown(props: TdUseCountDownProps, visibility?: Ref<boolean>): TdUseCountDown {
const {
time = 0,
autoStart,
Expand All @@ -18,27 +18,37 @@ export function useCountDown(props: TdUseCountDownProps): TdUseCountDown {
const fps = ref();
const count = ref(Number(time));
const showTimes = reactive(getShowTimes(getRemainTimes(time), format, millisecond, splitWithUnit));
let hiddenTime = 0;

// raf
const { pause, resume } = useRafFn(
async () => {
if (!isBrowser) return;
if (!fps.value) {
const res = await getScreenFps?.();
fps.value = res || 60;
}
count.value = parseInt(`${Number(count.value) - 1000 / fps.value}`, 10);
if (count.value <= 0) {
pause?.();
count.value = 0;
visibility &&
watch(visibility, (val) => {
if (val) {
count.value -= Date.now() - hiddenTime;
rafFn();
} else {
hiddenTime = Date.now();
}
const times = getRemainTimes(count.value);
onChange?.(times);
count.value === 0 && onFinish?.();
getShowTimes(times, format, millisecond, splitWithUnit)?.forEach?.((i, idx) => (showTimes[idx].value = i?.value));
},
{ immediate: autoStart },
);
});

const rafFn = async () => {
if (!isBrowser) return;
if (!fps.value) {
const res = await getScreenFps?.();
fps.value = res || 60;
}
count.value = parseInt(`${Number(count.value) - 1000 / fps.value}`, 10);
if (count.value <= 0) {
pause?.();
count.value = 0;
}
const times = getRemainTimes(count.value);
onChange?.(times);
count.value === 0 && onFinish?.();
getShowTimes(times, format, millisecond, splitWithUnit)?.forEach?.((i, idx) => (showTimes[idx].value = i?.value));
};

// raf
const { pause, resume } = useRafFn(rafFn, { immediate: autoStart });

/**
* return
Expand Down
Loading