Zamboni
Zamboni

Reputation: 335

Cancel All Currently Running RequestAnimationFrames

I am calling many requestAnimationFrames at the same time. How would I cancel all of the requestAnimationFrames such that none are running anymore?

Upvotes: 3

Views: 2804

Answers (2)

Unmitigated
Unmitigated

Reputation: 89254

You can call requestAnimationFrame, store the returned id, and then call cancelAnimationFrame for all positive integers less than that number.

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

Upvotes: 7

Graham P Heath
Graham P Heath

Reputation: 7399

window.requestAnimatioFrame(()=>{}) returns a number to be used with window.cancelAnimationFrame(number). So, store the numbers in an array, and then iterate over the array canceling the numbers.

Via MDN:

window.requestAnimatioFrame

window.cancelAnimationFrame

Upvotes: 2

Related Questions