2022-09-07 08:30:50 +00:00
|
|
|
export const frames = (fn) => {
|
|
|
|
let frameId = -1;
|
|
|
|
let lock = false;
|
|
|
|
|
2023-02-06 11:31:53 +00:00
|
|
|
const next = () => {
|
|
|
|
if (!lock) {
|
|
|
|
lock = true;
|
|
|
|
frameId = requestAnimationFrame(() => {
|
|
|
|
fn();
|
|
|
|
lock = false;
|
|
|
|
});
|
|
|
|
}
|
2022-09-07 08:30:50 +00:00
|
|
|
};
|
2023-02-06 11:31:53 +00:00
|
|
|
|
|
|
|
const cancel = () => {
|
|
|
|
cancelAnimationFrame(frameId);
|
|
|
|
lock = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
return { next, cancel };
|
2022-09-07 08:30:50 +00:00
|
|
|
};
|