Skip to content

Instantly share code, notes, and snippets.

@8mist
Created October 1, 2023 09:04
Show Gist options
  • Save 8mist/a9f0d6e83d3c7e7dc10749634705a732 to your computer and use it in GitHub Desktop.
Save 8mist/a9f0d6e83d3c7e7dc10749634705a732 to your computer and use it in GitHub Desktop.
const debounceToNextFrame = <F extends (...args: any[]) => void>(
fn: F,
): ((...funcArgs: Parameters<F>) => void) & {
cancel: () => void;
} => {
let frameReference: number;
const cancel = (): void => {
cancelAnimationFrame(frameReference);
};
return Object.assign(
(...args: Parameters<F>): void => {
cancel();
frameReference = requestAnimationFrame((): void => fn(...args));
},
{ cancel },
);
};
export default debounceToNextFrame;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment