Window: cancelAnimationFrame() 方法

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

window.cancelAnimationFrame() 方法會取消透過呼叫 window.requestAnimationFrame() 方法之前安排好的動畫幀請求。

語法

js
cancelAnimationFrame(requestID)

引數

requestID

呼叫 window.requestAnimationFrame() 請求回撥時返回的 ID 值。

返回值

無(undefined)。

示例

js
const start = document.timeline.currentTime;

let myReq;

function step(timestamp) {
  const progress = timestamp - start;
  d.style.left = `${Math.min(progress / 10, 200)}px`;
  if (progress < 2000) {
    // it's important to update the requestId each time you're calling requestAnimationFrame
    myReq = requestAnimationFrame(step);
  }
}
myReq = requestAnimationFrame(step);
// the cancellation uses the last requestId
cancelAnimationFrame(myReq);

規範

規範
HTML
# animationframeprovider-cancelanimationframe

瀏覽器相容性

另見