22 lines
360 B
TypeScript
22 lines
360 B
TypeScript
export const frames = (fn: Function) => {
|
|
let frameId = -1;
|
|
let lock = false;
|
|
|
|
const next = () => {
|
|
if (!lock) {
|
|
lock = true;
|
|
frameId = requestAnimationFrame(() => {
|
|
fn();
|
|
lock = false;
|
|
});
|
|
}
|
|
};
|
|
|
|
const cancel = () => {
|
|
cancelAnimationFrame(frameId);
|
|
lock = false;
|
|
};
|
|
|
|
return { next, cancel };
|
|
};
|