跳到主要内容

useRafFn

分类
导出大小
443 B
上次更改
8 个月前

在每个 requestAnimationFrame 上调用函数。带有暂停和恢复的控制。

演示

帧数: 0
增量: 0ms
FPS 限制: 60

用法

ts
import { 
useRafFn
} from '@vueuse/core'
import {
shallowRef
} from 'vue'
const
count
=
shallowRef
(0)
const {
pause
,
resume
} =
useRafFn
(() => {
count
.
value
++
console
.
log
(
count
.
value
)
})

类型声明

显示类型声明
ts
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

来源

源代码演示文档

贡献者

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

更新日志

v12.7.0
3041e - feat: 添加 once 选项 (#4583)
v12.1.0
4759f - feat: 允许帧率作为响应式值 (#4409)
0a9ed - feat!: 放弃对 Vue 2 的支持,优化打包并清理 (#4349)
v10.8.0
69990 - fix: 修复 fpsLimit 选项,修复 #3481,关闭 #3482
v10.5.0
8e4c0 - feat: 选项 fpsLimit (#3459)
v10.2.1
cf6eb - fix: 设置初始增量为零 (#3150)

根据 MIT 许可证发布。