Skip to content

watchThrottled

类别
导出体积
831 B
上次更改
last year
别名
throttledWatch

节流式监视。

示例

延迟设置为1000毫秒以进行演示。

输入内容:

更新次数: 0

用法

类似于 watch,但提供了额外的选项 throttle,该选项将应用于回调函数。

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

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

实质上,它相当于以下代码的简写:

typescript
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

更新日志

v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter