跳到主要內容

watchAtMost

類別
匯出大小
339 B
上次變更
5 天前

watch 搭配觸發次數。

用法

類似於 watch,具有額外選項 count,用於設定回呼函式觸發的次數。達到次數後,偵聽將自動停止。

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

watchAtMost(
  source,
  () => { console.log('trigger!') }, // triggered it at most 3 times
  {
    count: 3, // the number of times triggered
  },
)

類型宣告

typescript
export interface WatchAtMostOptions<Immediate>
  extends WatchWithFilterOptions<Immediate> {
  count: MaybeRefOrGetter<number>
}
export interface WatchAtMostReturn {
  stop: WatchStopHandle
  count: ShallowRef<number>
}
export declare function watchAtMost<
  T extends Readonly<WatchSource<unknown>[]>,
  Immediate extends Readonly<boolean> = false,
>(
  sources: [...T],
  cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>,
  options: WatchAtMostOptions<Immediate>,
): WatchAtMostReturn
export declare function watchAtMost<
  T,
  Immediate extends Readonly<boolean> = false,
>(
  sources: WatchSource<T>,
  cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
  options: WatchAtMostOptions<Immediate>,
): WatchAtMostReturn

原始碼

原始碼文件

貢獻者

Anthony Fu
Anthony Fu
IlyaL
vaakian X
lvjiaxuan
lvjiaxuan
HG
webfansplz

更新日誌

v12.8.0 於 2025/3/5
7432f - 特性(types): 棄用 MaybeRefMaybeRefOrGetter,改用 Vue 原生 (#4636)
v12.3.0 於 2025/1/2
59f75 - 特性(toValue): 棄用 toValue,改用 Vue 原生
v12.0.0-beta.1 於 2024/11/21
0a9ed - 特性!: 移除 Vue 2 支援、最佳化套件並清理 (#4349)
v10.0.0-beta.4 於 2023/4/13
4d757 - 特性(types)!: 重新命名 MaybeComputedRefMaybeRefOrGetter
0a72b - 特性(toValue): 重新命名 resolveUnreftoValue

以 MIT 授權條款發布。