Skip to content
On this page

watchAtMost

指定 watch 触发的次数。

watch with the number of times triggered.

Usage

类似于 watch,带有一个额外的选项 countcount 可以设置回调函数触发的次数。到达计数后,watch将自动停止。

Similar to watch with an extra option count which set up the number of times the callback function is triggered. After the count is reached, the watch will be stopped automatically.

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

watchAtMost(
  source,
  () => { console.log('trigger!') }, // 最多触发3次
  {
    count: 3, // 触发次数
  },
)
import { watchAtMost } from '@vueuse/core'

watchAtMost(
  source,
  () => { console.log('trigger!') }, // 最多触发3次
  {
    count: 3, // 触发次数
  },
)

Type Declarations

typescript
export interface WatchAtMostOptions<Immediate>
  extends WatchWithFilterOptions<Immediate> {
  count: MaybeComputedRef<number>
}
export interface WatchAtMostReturn {
  stop: WatchStopHandle
  count: Ref<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
export interface WatchAtMostOptions<Immediate>
  extends WatchWithFilterOptions<Immediate> {
  count: MaybeComputedRef<number>
}
export interface WatchAtMostReturn {
  stop: WatchStopHandle
  count: Ref<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

Source

Category
Export Size
561 B
Last Changed
7 months ago

SourceDocs

贡献者(Contributors)

日志(Changelog)

No recent changes

Released under the MIT License.