跳到主要內容

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://developer.mozilla.org/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.dev.org.tw/useRafFn
 * @param fn
 * @param options
 */
export declare function useRafFn(
  fn: (args: UseRafFnCallbackArguments) => void,
  options?: UseRafFnOptions,
): Pausable

原始碼

原始碼示範文件

貢獻者

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 於 2025/2/15
3041e - feat: 新增 once 選項 (#4583)
v12.1.0 於 2024/12/22
4759f - feat: 允許幀率為響應式數值 (#4409)
v12.0.0-beta.1 於 2024/11/21
0a9ed - feat!: 移除 Vue 2 支援,最佳化 bundles 並清理 (#4349)
v10.8.0 於 2024/2/20
69990 - fix: 修復 fpsLimit 選項,修復 #3481, 關閉 #3482
v10.5.0 於 2023/10/7
8e4c0 - feat: 選項 fpsLimit (#3459)
v10.2.1 於 2023/6/28
cf6eb - fix: 設定初始 delta 為零 (#3150)
v9.12.0 於 2023/1/29
39183 - feat(useRafFn, useIntervalFn, useTimeoutFn): 使狀態唯讀 (#2685)

根據 MIT 許可證發布。