Skip to content

useThrottleFn

Category
Export Size
471 B
Last Changed
last week
Related

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

Throttle is a spring that throws balls: after a ball flies out it needs some time to shrink back, so it cannot throw any more balls unless it's ready.

Demo

Delay is set to 1000ms for this demo.

Button clicked: 0

Event handler called: 0

Usage

ts
import { 
useThrottleFn
} from '@vueuse/core'
const
throttledFn
=
useThrottleFn
(() => {
// do something, it will be called at most 1 time per second }, 1000) useEventListener(
window
, 'resize',
throttledFn
)

Type Declarations

Show Type Declarations
ts
/**
 * Throttle execution of a function. Especially useful for rate limiting
 * execution of handlers on events like resize and scroll.
 *
 * @param   fn             A function to be executed after delay milliseconds. The `this` context and all arguments are passed through, as-is,
 *                                    to `callback` when the throttled-function is executed.
 * @param   ms             A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
 *                                    (default value: 200)
 *
 * @param [trailing] if true, call fn again after the time is up (default value: false)
 *
 * @param [leading] if true, call fn on the leading edge of the ms timeout (default value: true)
 *
 * @param [rejectOnCancel] if true, reject the last call if it's been cancel (default value: false)
 *
 * @return  A new, throttled, function.
 *
 * @__NO_SIDE_EFFECTS__
 */
export declare function 
useThrottleFn
<
T
extends
FunctionArgs
>(
fn
:
T
,
ms
?:
MaybeRefOrGetter
<number>,
trailing
?: boolean,
leading
?: boolean,
rejectOnCancel
?: boolean,
):
PromisifyFn
<
T
>

Source

SourceDemoDocs

Contributors

Anthony Fu
SerKo
Anthony Fu
Thimo Sietsma
IlyaL
Fernando Fernández
OrbisK
Ctibor Laky
丶远方
vaakian X
azaleta
webfansplz
Jakub Freisler
Roman Harmyder
wheat

Changelog

v13.6.0 on
d32f8 - refactor: add @__NO_SIDE_EFFECTS__ annotations to all pure functions (#4907)
v12.8.0 on
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
v10.0.0-beta.4 on
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter

Released under the MIT License.

Build faster with AI
New Masterclass to help you leverage AI in your Vue workflow
Get Early Access