大网数据广告活动
Web 标准与前端开发

XRSession: cancelAnimationFrame() method

· 52CE 我爱测

The **`cancelAnimationFrame()`** method of the XRSession interface cancels an animation frame which was previously requested by calling XRSession.requestAnimationFrame.

WebXR Device API

The cancelAnimationFrame() method of the XRSession interface cancels an animation frame which was previously requested by calling XRSession.requestAnimationFrame.

Syntax

cancelAnimationFrame(handle)

Parameters

  • handle
    • : The unique value returned by the call to XRSession.requestAnimationFrame that previously scheduled the animation callback.

Return value

None (undefined).

Usage notes

This function has no effect if the specified handle cannot be found.

Examples

In the example below we see code which starts up a WebXR session if immersive VR mode is supported. Once started, the session schedules its first frame to be rendered by calling XRSession.requestAnimationFrame.

The pauseXR() function shown at the bottom can be called to suspend the WebVR session, in essence, by canceling any pending animation frame callback. Since each frame callback schedules the next one, removing the callback terminates updating of the WebXR scene.

const XR = navigator.xr;

let requestHandle = null;
let xrSession = null;

if (XR) {
  XR.isSessionSupported("immersive-vr").then((isSupported) => {
    if (isSupported) {
      startXR();
    }
  });
}

function frameCallback(time, xrFrame) {
  xrSession.requestAnimationFrame(frameCallback);

  // Update and render the frame
}

async function startXR() {
  xrSession = XR.requestSession("immersive-vr");

  if (xrSession) {
    stopButton.onclick = stopXR;
    requestHandle = xrSession.requestAnimationFrame(frameCallback);
  }
}

function pauseXR() {
  if (xrSession && requestHandle) {
    xrSession.cancelAnimationFrame(requestHandle);
    requestHandle = null;
  }
}

Specifications

Browser compatibility

See also

  • Window.cancelAnimationFrame
  • XRSession.requestAnimationFrame

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。