From d5898a01733f9e1fcea318b0eea9644643dddd5b Mon Sep 17 00:00:00 2001 From: Athou Date: Sat, 24 Jun 2023 23:04:52 +0200 Subject: [PATCH] throttle scroll listener --- commafeed-client/src/app/utils.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/commafeed-client/src/app/utils.ts b/commafeed-client/src/app/utils.ts index ba31235f..09ad0c16 100644 --- a/commafeed-client/src/app/utils.ts +++ b/commafeed-client/src/app/utils.ts @@ -1,3 +1,4 @@ +import { throttle } from "throttle-debounce" import { Category } from "./types" export function visitCategoryTree(category: Category, visitor: (category: Category) => void): void { @@ -31,15 +32,15 @@ export const scrollToWithCallback = ({ options, onScrollEnded }: { options: Scro const onScroll = () => { if (window.scrollY.toFixed() === offset) { - window.removeEventListener("scroll", onScroll) + window.removeEventListener("scroll", throttleListener) onScrollEnded() } } - - window.addEventListener("scroll", onScroll) + const throttleListener = throttle(100, onScroll) + window.addEventListener("scroll", throttleListener) // scrollTo does not trigger if there's nothing to do, trigger it manually - onScroll() + throttleListener() window.scrollTo(options) }