取消所有当前 运行 个 RequestAnimationFrames

Cancel All Currently Running RequestAnimationFrames

我同时调用了很多requestAnimationFrames。我将如何取消所有 requestAnimationFrames 使得 none 不再是 运行?

window.requestAnimatioFrame(()=>{}) returns 与 window.cancelAnimationFrame(number) 一起使用的数字。因此,将数字存储在数组中,然后遍历数组以取消数字。

通过 MDN:

window.requestAnimatioFrame

window.cancelAnimationFrame

您可以调用 requestAnimationFrame,存储返回的 ID,然后为小于该数字的所有正整数调用 cancelAnimationFrame

function cancelAllAnimationFrames(){
   var id = window.requestAnimationFrame(function(){});
   while(id--){
     window.cancelAnimationFrame(id);
   }
}