|
1 | 1 | import { EventHandler } from "./bootstrap-helper"; |
2 | | - |
3 | | -/** |
4 | | - * Throttles a function so it's called at most once during a specified delay. |
5 | | - * |
6 | | - * @param {function} func The function to throttle. |
7 | | - * @param {number} delay The delay in milliseconds. |
8 | | - * @return {function} The throttled function. |
9 | | - */ |
10 | | -function throttle(func, delay) { |
11 | | - let timeoutId; |
12 | | - let lastArgs; |
13 | | - let lastThis; |
14 | | - let calledDuringDelay = false; |
15 | | - |
16 | | - return function (...args) { |
17 | | - lastArgs = args; |
18 | | - lastThis = this; |
19 | | - |
20 | | - if (!timeoutId) { |
21 | | - func.apply(lastThis, lastArgs); |
22 | | - calledDuringDelay = false; |
23 | | - timeoutId = setTimeout(() => { |
24 | | - timeoutId = null; |
25 | | - if (calledDuringDelay) { |
26 | | - func.apply(lastThis, lastArgs); |
27 | | - calledDuringDelay = false; |
28 | | - } |
29 | | - }, delay); |
30 | | - } else { |
31 | | - calledDuringDelay = true; |
32 | | - } |
33 | | - }; |
34 | | -} |
| 2 | +import { throttle } from "@asu/shared"; |
35 | 3 |
|
36 | 4 | /** |
37 | 5 | * Initializes the anchor menu functionality. |
@@ -179,9 +147,7 @@ function initAnchorMenu() { |
179 | 147 | previousScrollPosition = window.scrollY; |
180 | 148 | }; |
181 | 149 |
|
182 | | - const throttledScrollHandler = throttle(scrollHandlerLogic, SCROLL_DELAY); |
183 | | - |
184 | | - window.addEventListener("scroll", throttledScrollHandler, { passive: true }); |
| 150 | + window.addEventListener("scroll", () => throttle(scrollHandlerLogic, SCROLL_DELAY), { passive: true }); |
185 | 151 |
|
186 | 152 | // Set click event of anchors |
187 | 153 | for (let [anchor, anchorTarget] of anchorTargets) { |
|
0 commit comments