This feature is not Baseline because it does not work in some of the most widely-used browsers.
Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
The cancelAnimationFrame() method of the XRSession interface cancels an animation frame which was previously requested by calling requestAnimationFrame.
cancelAnimationFrame(handle)
handleThe unique value returned by the call to requestAnimationFrame() that previously scheduled the animation callback.
None (undefined).
This function has no effect if the specified handle cannot be found.
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 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;
}
}
| Specification |
|---|
| WebXR Device API> # dom-xrsession-cancelanimationframe> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
cancelAnimationFrame |
79 | 79 | No | 66 | No | 79 | No | 57 | No | 11.2 | No | No |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/XRSession/cancelAnimationFrame