From ca1b25a9db22dc68f8f6fe954b83837830154bed Mon Sep 17 00:00:00 2001 From: wxxFE <2022095028@qq.com> Date: Sat, 24 Aug 2024 15:14:39 +0800 Subject: [PATCH] fix: streamline the process --- src/hooks/useTarget.ts | 35 ++++++++++++----------------------- src/util.ts | 13 ------------- 2 files changed, 12 insertions(+), 36 deletions(-) diff --git a/src/hooks/useTarget.ts b/src/hooks/useTarget.ts index 80eac13..cf2205c 100644 --- a/src/hooks/useTarget.ts +++ b/src/hooks/useTarget.ts @@ -2,7 +2,7 @@ import useEvent from 'rc-util/lib/hooks/useEvent'; import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect'; import { useMemo, useState } from 'react'; import type { TourStepInfo } from '..'; -import { isInViewPort,debounce } from '../util'; +import { isInViewPort } from '../util'; export interface Gap { offset?: number | [number, number]; @@ -62,37 +62,26 @@ export default function useTarget( } }); - - let ticking = false; - const debounceUpdatePos = debounce(updatePos,1000/60*2); - let a = [] - const scrollUpdatePos = window.requestAnimationFrame ? ()=>{ - if (!ticking) { - window.requestAnimationFrame(() => { - debounceUpdatePos(); - a.push(Date.now()) - ticking = false; - }); - ticking = true; - } - console.log(a) - - }: debounce(updatePos,1000/60*3) - - - const getGapOffset = (index: number) => (Array.isArray(gap?.offset) ? gap?.offset[index] : gap?.offset) ?? 6; + const scrollEndUpdatePos = ()=>{ + if(window.requestAnimationFrame){ + window.requestAnimationFrame(updatePos); + return; + } + setTimeout(updatePos,1000/60); + } + useLayoutEffect(() => { updatePos(); // update when window resize window.addEventListener('resize', updatePos); - // update when window scroll stop - window.addEventListener('scroll',scrollUpdatePos) + // update when window scroll end + window.addEventListener('scrollend',scrollEndUpdatePos); return () => { window.removeEventListener('resize', updatePos); - window.removeEventListener('scroll', scrollUpdatePos); + window.removeEventListener('scrollend',scrollEndUpdatePos); }; }, [targetElement, open, updatePos]); diff --git a/src/util.ts b/src/util.ts index 88d243b..cc95084 100644 --- a/src/util.ts +++ b/src/util.ts @@ -18,16 +18,3 @@ export function getPlacement( stepPlacement ?? placement ?? (targetElement === null ? 'center' : 'bottom') ); } - -export function debounce any>(func: T, wait: number): T { - let timeoutId: ReturnType; - - const debouncedFunc = (...args: Parameters): void => { - clearTimeout(timeoutId); - timeoutId = setTimeout(() => { - func(...args); - }, wait); - }; - - return debouncedFunc as T; -} \ No newline at end of file