react-anchor-navigation
Version:
React lightweight library for anchor scrolling and navigation tied to URL hash.
22 lines (18 loc) • 402 B
text/typescript
export function throttle(fn: Function, delay: number) {
let timeout: ReturnType<typeof setTimeout> | null = null;
function throttledFn() {
if (!timeout) {
timeout = setTimeout(() => {
timeout = null;
fn();
}, delay);
}
}
throttledFn.cancel = () => {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
};
return throttledFn;
}