diff --git a/apps/admin-x-settings/src/utils/IframeBuffering.tsx b/apps/admin-x-settings/src/utils/IframeBuffering.tsx index e73582ce49..1f694207e7 100644 --- a/apps/admin-x-settings/src/utils/IframeBuffering.tsx +++ b/apps/admin-x-settings/src/utils/IframeBuffering.tsx @@ -11,6 +11,19 @@ type IframeBufferingProps = { addDelay?: boolean; }; +function debounce(func: any, wait: number) { // eslint-disable-line + let timeout: NodeJS.Timeout; + + return function executedFunction(...args: any) { // eslint-disable-line + const later = () => { + clearTimeout(timeout); + func(...args); + }; + clearTimeout(timeout); + timeout = setTimeout(later, wait); + }; +} + const IframeBuffering: React.FC = ({generateContent, className, height, width, parentClassName, testId, addDelay = false}) => { const [visibleIframeIndex, setVisibleIframeIndex] = useState(0); const iframes = [useRef(null), useRef(null)]; // eslint-disable-line @@ -48,9 +61,10 @@ const IframeBuffering: React.FC = ({generateContent, class useEffect(() => { const iframe = iframes[visibleIframeIndex].current; - const onScroll = () => { + + const onScroll = debounce(() => { setScrollPosition(iframe?.contentWindow?.scrollY || 0); - }; + }, 250); iframe?.contentWindow?.addEventListener('scroll', onScroll);