> For AI agents: the complete documentation index is available at /next/zh/llms.txt, the full documentation bundle is available at /next/zh/llms-full.txt.

# requestAnimationFrame()

APISummary
在下一次 VSYNC 时调用指定回调函数。

`requestAnimationFrame` 通常用于希望通过在每一帧内修改属性来实现动画效果的场景。
注意 `requestAnimationFrame` 只会在下一帧生效；如果希望使其持续生效，需要嵌套调用 `requestAnimationFrame`。

## 语法

```ts
requestAnimationFrame: (callback: (timeStamp: number) => void) => number;
```

### 参数

#### callback

在下一次 VSYNC 时，需要调用的回调函数。回调函数具有一个参数 `timeStamp`，是开始执行回调函数的时刻。

### 返回值

一个整数类型的 id，用于取消这个 `requestAnimationFrame` 的回调。

## 示例

在控制台中打印帧率：

**下面是一个示例: lynx-api**

**Bundle:** `dist/request-animation-frame.lynx.bundle` | Web: `dist/request-animation-frame.web.bundle`

```tsx
import { root, useEffect, useRef, useState } from "@lynx-js/react";

import "./index.css";

import arrow from "../../resource/arrow.png";
import lynxLogo from "../../resource/lynxlogo.png";

const RequestAnimationFrame = () => {
  const lastUptimeRef = useRef(0);
  const framesRef = useRef(0);
  const [fpsStr, setFpsStr] = useState("0");

  function drawFrame(timestamp: number) {
    requestAnimationFrame(drawFrame);
    framesRef.current++;
    const time = Math.floor(timestamp / 3000) | 0;

    if (time !== lastUptimeRef.current) {
      setFpsStr(String(Math.floor(framesRef.current / 3)));
      console.log("FPS: " + fpsStr);
      lastUptimeRef.current = time;
      framesRef.current = 0;
    }
  }
  useEffect(() => {
    requestAnimationFrame(drawFrame);
  }, []);
  return (
    <page>
      <view className="Background" />
      <view className="App">
        <view className="Banner">
          <view className="Logo">
            <image src={lynxLogo} className="Logo--lynx" />
          </view>
          <text className="Title">React</text>
          <text className="Subtitle">on Lynx</text>
        </view>
        <view className="Content">
          <image src={arrow} className="Arrow" />
          <text className="Description">{"FPS: " + fpsStr}</text>
          <text className="Hint">
            Edit<text style={{ fontStyle: "italic", color: "rgba(255, 255, 255, 0.85)" }}>{" src/App.tsx "}</text>
            to see updates!
          </text>
        </view>
        <view style={{ flex: 1 }}></view>
      </view>
    </page>
  );
};

root.render(<RequestAnimationFrame />);

```

v0.2.1



## 兼容性

**Compatibility Table**

**Query:** `lynx-api.lynx.requestAnimationFrame`

[Source](https://github.com/lynx-family/lynx-website/tree/main/packages/lynx-compat-data/lynx-api/lynx/requestAnimationFrame.json)

| Feature | Android | HarmonyOS | iOS | macOS | Windows | Web |
| --- | --- | --- | --- | --- | --- | --- |
| requestAnimationFrame | Yes (1.6) | Yes (3.4) | Yes (1.6) | Yes (3.5) | Yes (3.5) | Yes |

**`requestAnimationFrame`**

Invoke the specified callback function at the next VSYNC.

**Specifications:**

