cancelanimationframe

Is there a way to cancel requestAnimationFrame without a global variable?

瘦欲@ 提交于 2021-02-04 21:34:46
问题 I'm trying to cancel a requestAnimationFrame loop, but I can't do it because each time requestAnimationFrame is called, a new timer ID is returned, but I only have access to the return value of the first call to requestAnimationFrame . Specifically, my code is like this, which I don't think is entirely uncommon: function animate(elem) { var step = function (timestamp) { //Do some stuff here. if (progressedTime < totalTime) { return requestAnimationFrame(step); //This return value seems