How to stop a requestAnimationFrame recursion/loop? How to stop a requestAnimationFrame recursion/loop? javascript javascript

How to stop a requestAnimationFrame recursion/loop?


One way to start/stop is like this

var requestId;function loop(time) {    requestId = undefined;    ...    // do stuff    ...    start();}function start() {    if (!requestId) {       requestId = window.requestAnimationFrame(loop);    }}function stop() {    if (requestId) {       window.cancelAnimationFrame(requestId);       requestId = undefined;    }}

Working example: