HTMLVideoElement: cancelVideoFrameCallback() 方法

Baseline 2024
新推出

自 2024 年 10 月以來,此功能已可在最新的裝置和瀏覽器版本上使用。此功能可能不適用於舊裝置或瀏覽器。

HTMLVideoElement 介面的 cancelVideoFrameCallback() 方法用於取消先前註冊的影片幀回撥。

語法

js
cancelVideoFrameCallback(id)

引數

id

一個代表您要取消的影片幀回撥 ID 的數字。這將是相應 HTMLVideoElement.requestVideoFrameCallback 呼叫返回的值。

返回值

無(undefined)。

示例

取消影片幀回撥

此示例展示瞭如何使用 cancelVideoFrameCallback() 來取消先前註冊的影片幀回撥。

js
let videoCallbackId = null;

function updateCanvas(now, metadata) {
  // Do something with the frame

  // …

  // Re-register the callback to run on the next frame
  // It's important to update the videoCallbackId on each iteration
  // so you can cancel the callback successfully
  videoCallbackId = video.requestVideoFrameCallback(updateCanvas);
}

// Initial registration of the callback to run on the first frame
videoCallbackId = video.requestVideoFrameCallback(updateCanvas);

// …

// Cancel video frame callback using the latest videoCallbackId
if (videoCallbackId !== null) {
  video.cancelVideoFrameCallback(videoCallbackId);
}

規範

規範
HTMLVideoElement.requestVideoFrameCallback()
# dom-htmlvideoelement-cancelvideoframecallback

瀏覽器相容性

另見