Skip to content
Utilities

useThrottleFn

Throttle execution of a function. Especially useful for rate limiting execution of handlers on events like resize and scroll.

Throttle execution of a function. Especially useful for rate limiting execution of handlers on events like resize and scroll.

useThrottleFn
1000ms limit

Limits function execution to at most once per 1000ms, no matter how fast you click.

Clicks
0
Throttled Calls
0
import {
import useThrottleFn
useThrottleFn
} from "@usels/web";
function
function Component(): void
Component
() {
const
const throttledFn: any
throttledFn
=
import useThrottleFn
useThrottleFn
((
value: string
value
: string) => {
any
console
.
any
log
(
value: string
value
);
}, 250);
}
import { useObservable, useThrottleFn } from "@usels/web";
function Component() {
const delay$ = useObservable(300);
const throttledFn = useThrottleFn(() => {
// ...
}, delay$);
// Changing delay$ applies the new interval from the next call
}
import { useThrottleFn } from "@usels/web";
function Component() {
// Fires immediately on first call, suppresses all subsequent calls within interval
const throttledFn = useThrottleFn(
() => { /* ... */ },
300,
{ edges: ["leading"] }
);
}
import { useThrottleFn } from "@usels/web";
function Component() {
// Does not fire immediately — fires once after the interval ends
const throttledFn = useThrottleFn(
() => { /* ... */ },
300,
{ edges: ["trailing"] }
);
}

View on GitHub