Skip to content

Commit 6ccc03c

Browse files
committed
adjust debounce logic
1 parent 0f32831 commit 6ccc03c

File tree

2 files changed

+6
-14
lines changed

2 files changed

+6
-14
lines changed

src/components/track-reader-depth.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export function ReaderDepthTracker() {
5353
if (document.documentElement.scrollHeight - window.innerHeight === 0) {
5454
return () => {};
5555
}
56-
const debouncedTrackProgress = debounce(trackProgress, 20);
56+
const debouncedTrackProgress = debounce(trackProgress, 50);
5757

5858
window.addEventListener('scroll', debouncedTrackProgress);
5959
return () => {

src/utils.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,10 @@ export const stripTrailingSlash = (url: string) => {
115115
* @returns A debounced function.
116116
* @see https://davidwalsh.name/javascript-debounce-function
117117
*/
118-
export function debounce(func: Function, wait = 20, immediate = true) {
119-
let timeout: ReturnType<typeof setTimeout> | null;
120-
return function () {
121-
const context = this;
122-
const args = arguments;
123-
const later = function () {
124-
timeout = null;
125-
if (!immediate) func.apply(context, args);
126-
};
127-
const callNow = immediate && !timeout;
128-
if (timeout) clearTimeout(timeout);
129-
timeout = setTimeout(later, wait);
130-
if (callNow) func.apply(context, args);
118+
export function debounce<T extends unknown[]>(func: (...args: T) => void, delay: number) {
119+
let timer: ReturnType<typeof setTimeout>;
120+
return function (...args: T) {
121+
clearTimeout(timer);
122+
timer = setTimeout(() => func.apply(this, args), delay);
131123
};
132124
}

0 commit comments

Comments
 (0)