watchThrottled
節流 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 }
原始碼
貢獻者
更新日誌
v12.8.0
於 2025/3/5v12.0.0-beta.1
於 2024/11/21v10.0.0-beta.4
於 2023/4/134d757
- feat(types)!: 將 MaybeComputedRef
重新命名為 MaybeRefOrGetter