コンテンツにスキップ

useThrottleFn

カテゴリ
エクスポートサイズ
401 B
最終更新日
6ヶ月前
関連

関数のthrottle実行。リサイズやスクロールなどのイベントハンドラのレート制限に特に役立ちます。

Throttleはボールを投げるバネのようなものです。ボールが飛び出した後、縮むのに時間がかかるため、準備が整うまでそれ以上のボールを投げることはできません。

デモ

このデモでは、遅延は1000ミリ秒に設定されています。

ボタンクリック回数: 0

イベントハンドラ呼び出し回数: 0

使用方法

js
import { useThrottleFn } from '@vueuse/core'

const throttledFn = useThrottleFn(() => {
  // do something, it will be called at most 1 time per second
}, 1000)

window.addEventListener('resize', throttledFn)

型宣言

型宣言を表示
typescript
/**
 * 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.
 */
export declare function useThrottleFn<T extends FunctionArgs>(
  fn: T,
  ms?: MaybeRefOrGetter<number>,
  trailing?: boolean,
  leading?: boolean,
  rejectOnCancel?: boolean,
): PromisifyFn<T>

ソース

ソースデモドキュメント

貢献者

Anthony Fu
Anthony Fu
Ctibor Laky
丶远方
vaakian X
azaleta
webfansplz
Jakub Freisler
Roman Harmyder
wheat

変更履歴

v10.0.0-beta.4 2023年4月13日
4d757 - 機能追加(types)!: MaybeComputedRefMaybeRefOrGetter に名前変更
v9.10.0 2023年1月3日
4d305 - 機能追加(useDebounceFn,useThrottleFn): Promise を使用して結果を返す (#2580)

MITライセンスで公開されています。