Files
30-seconds-of-code/test/recordAnimationFrames/recordAnimationFrames.js
2018-06-18 19:07:48 +03:00

21 lines
390 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;