Skip to content

useRafFn

类别
导出体积
342 B
上次更改
2 minutes ago

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

示例

帧数:0
间隔时间:0毫秒
FPS 限制:60

用法

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

const count = ref(0)

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

类型声明

typescript
export interface UseRafFnCallbackArguments {
  /**
   * 当前帧与上一帧之间经过的时间。
   */
  delta: number
  /**
   * 自网页创建以来经过的时间。参见 {@link https://developer.mozilla.org/en-US/docs/Web/API/DOMHighResTimeStamp#the_time_origin 时间起点}
   */
  timestamp: DOMHighResTimeStamp
}
export interface UseRafFnOptions extends ConfigurableWindow {
  /**
   * 在创建时立即开始 requestAnimationFrame 循环。
   *
   * @default true
   */
  immediate?: boolean
  /**
   * 每秒执行函数的最大帧数。
   * 设置为 `undefined` 来禁用限制。
   *
   * @default undefined
   */
  fpsLimit?: number
}
/**
 * 在每次 `requestAnimationFrame` 上调用函数。具有暂停和恢复控制。
 *
 * @see https://vueuse.org/useRafFn
 * @param fn
 * @param options
 */
export declare function useRafFn(
  fn: (args: UseRafFnCallbackArguments) => void,
  options?: UseRafFnOptions,
): Pausable

源码

源码演示文档

贡献者

Anthony Fu
一纸忘忧
Anthony Fu
max.lee
Hayk Khachatryan
Dan Rose
Enzo Innocenzi
Jelf
he yifan
Alex Kozack
Jacob Clevenger
Scott Bedard
Antério Vieira

更新日志

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)
v9.7.0 on 12/16/2022
692d0 - feat: add delta and timestamp (#2493)