跳到主要内容

useRafFn

类别
导出大小
385 B
最后更改
3 周前

在每次 requestAnimationFrame 时调用函数。具有暂停和恢复的控制。

演示

帧数: 0
Delta: 0ms
FPS 限制: 60

使用方法

js
import { useRafFn } from '@vueuse/core'
import { shallowRef } from 'vue'

const count = shallowRef(0)

const { pause, resume } = useRafFn(() => {
  count.value++
  console.log(count.value)
})

类型声明

显示类型声明
typescript
export interface UseRafFnCallbackArguments {
  /**
   * Time elapsed between this and the last frame.
   */
  delta: number
  /**
   * Time elapsed since the creation of the web page. See {@link https://mdn.org.cn/en-US/docs/Web/API/DOMHighResTimeStamp#the_time_origin Time origin}.
   */
  timestamp: DOMHighResTimeStamp
}
export interface UseRafFnOptions extends ConfigurableWindow {
  /**
   * Start the requestAnimationFrame loop immediately on creation
   *
   * @default true
   */
  immediate?: boolean
  /**
   * The maximum frame per second to execute the function.
   * Set to `undefined` to disable the limit.
   *
   * @default undefined
   */
  fpsLimit?: MaybeRefOrGetter<number>
  /**
   * After the requestAnimationFrame loop executed once, it will be automatically stopped.
   *
   * @default false
   */
  once?: boolean
}
/**
 * Call function on every `requestAnimationFrame`. With controls of pausing and resuming.
 *
 * @see https://vueuse.org.cn/useRafFn
 * @param fn
 * @param options
 */
export declare function useRafFn(
  fn: (args: UseRafFnCallbackArguments) => void,
  options?: UseRafFnOptions,
): Pausable

源码

SourceDemoDocs

贡献者

Anthony Fu
Anthony Fu
IlyaL
btea
Robin
OrbisK
max.lee
Hayk Khachatryan
Dan Rose
Enzo Innocenzi
Jelf
he yifan
Alex Kozack
Jacob Clevenger
Scott Bedard
Antério Vieira

更新日志

v12.7.0 on 2/15/2025
3041e - feat: add once option (#4583)
v12.1.0 on 12/22/2024
4759f - feat: allow framerate to be a reactive value (#4409)
v12.0.0-beta.1 on 11/21/2024
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v10.8.0 on 2/20/2024
69990 - fix: fix fpsLimit option, fix #3481, close #3482
v10.5.0 on 10/7/2023
8e4c0 - feat: option fpsLimit (#3459)
v10.2.1 on 6/28/2023
cf6eb - fix: set initial delta to zero (#3150)
v9.12.0 on 1/29/2023
39183 - feat(useRafFn, useIntervalFn, useTimeoutFn): make status readonly (#2685)

在 MIT 许可证下发布。