Skip to content
Advertisement

how to restart a loop requestAnimationFrame

I’m tring to make a game and i want to use window.requestAnimationFrame() to make roles move, the movement seems to be right at first.

but there is problem that when i re-trigger move() immediately after triggering pause(), the movement will be faster and faster, i have code blow, move()pause() and initListener() are functions about movement

JavaScript

I guess it’s because when i re-trigger move(), there are still some requestAnimationFrame() running in javascript stack?

how can I fix this?

Advertisement

Answer

You should either unregister your event listeners when doing a pause – or register them outside of the move() function (e.g. in mount())

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement