コンテンツにスキップ

watchThrottled

カテゴリ
エクスポートサイズ
831 バイト
最終更新日
先週
エイリアス
throttledWatch

throttleされたwatch。

デモ

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

入力

更新回数: 0

使用方法

watchと似ていますが、コールバック関数に適用される追加オプション throttle が提供されます。

ts
import { watchThrottled } from '@vueuse/core'

watchThrottled(
  source,
  () => { console.log('changed!') },
  { throttle: 500 },
)

これは本質的に次のコードの省略形です

ts
import { throttleFilter, watchWithFilter } from '@vueuse/core'

watchWithFilter(
  source,
  () => { console.log('changed!') },
  {
    eventFilter: throttleFilter(500),
  },
)

型宣言

型宣言を表示
typescript
export interface WatchThrottledOptions<Immediate>
  extends WatchOptions<Immediate> {
  throttle?: MaybeRefOrGetter<number>
  trailing?: boolean
  leading?: boolean
}
export declare function watchThrottled<
  T extends Readonly<WatchSource<unknown>[]>,
  Immediate extends Readonly<boolean> = false,
>(
  sources: [...T],
  cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>,
  options?: WatchThrottledOptions<Immediate>,
): WatchStopHandle
export declare function watchThrottled<
  T,
  Immediate extends Readonly<boolean> = false,
>(
  source: WatchSource<T>,
  cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
  options?: WatchThrottledOptions<Immediate>,
): WatchStopHandle
export declare function watchThrottled<
  T extends object,
  Immediate extends Readonly<boolean> = false,
>(
  source: T,
  cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
  options?: WatchThrottledOptions<Immediate>,
): WatchStopHandle
export { watchThrottled as throttledWatch }

ソース

ソースデモドキュメント

貢献者

Anthony Fu
sun0day
Anthony Fu
丶遠方
lvjiaxuan
Joe Zimmerman
webfansplz

変更履歴

v12.0.0-beta.1 2024/11/21
0a9ed - feat!: Vue 2 サポートの削除、バンドルの最適化、クリーンアップ (#4349)
v10.0.0-beta.4 2023/4/13
4d757 - feat(types)!: MaybeComputedRef から MaybeRefOrGetter に名前変更

MITライセンスの下でリリースされています。