取消動畫
要取消對 requestAnimationFrame
的呼叫,你需要從上次呼叫時返回的 ID。這是你用於 cancelAnimationFrame
的引數。以下示例啟動一些假設動畫,然後在一秒鐘後暫停它。
// stores the id returned from each call to requestAnimationFrame
var requestId;
// draw something
function draw(timestamp) {
// do some animation
// request next frame
start();
}
// pauses the animation
function pause() {
// pass in the id returned from the last call to requestAnimationFrame
cancelAnimationFrame(requestId);
}
// begin the animation
function start() {
// store the id returned from requestAnimationFrame
requestId = requestAnimationFrame(draw);
}
// begin now
start();
// after a second, pause the animation
setTimeout(pause,1000);