Skip to content

Commit d39932e

Browse files
committed
Changed the throttle so the function fires at the end of the period, not immediately when first called
1 parent 9bcb8cb commit d39932e

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

apps/webapp/app/utils/throttle.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
//From: https://kettanaito.com/blog/debounce-vs-throttle
22

3-
/** A very simple throttle. Will execute the function every Xms and discard any other calls during that period. */
3+
/** A very simple throttle. Will execute the function at the end of each period and discard any other calls during that period. */
44
export function throttle(
55
func: (...args: any[]) => void,
6-
duration: number
6+
durationMs: number
77
): (...args: any[]) => void {
8-
let shouldWait = false;
8+
let isPrimedToFire = false;
99

1010
return (...args: any[]) => {
11-
if (!shouldWait) {
12-
func(...args);
13-
shouldWait = true;
11+
if (!isPrimedToFire) {
12+
isPrimedToFire = true;
1413

1514
setTimeout(() => {
16-
shouldWait = false;
17-
}, duration);
15+
func(...args);
16+
isPrimedToFire = false;
17+
}, durationMs);
1818
}
1919
};
2020
}

0 commit comments

Comments
 (0)