Files
30-seconds-of-code/test/recordAnimationFrames/recordAnimationFrames.js
2018-08-02 13:49:33 +03:00

22 lines
449 B
JavaScript

const recordAnimationFrames = (callback, autoStart = true) => {
let running = true,
raf;
const stop = () => {
running = false;
cancelAnimationFrame(raf);
};
const start = () => {
running = true;
run();
};
const run = () => {
raf = requestAnimationFrame(() => {
callback();
if (running) run();
});
};
if (autoStart) start();
return { start, stop };
};
module.exports = recordAnimationFrames;