Skip to content

Instantly share code, notes, and snippets.

@jakubbarczyk
Last active December 25, 2023 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakubbarczyk/36cc580b1a4ec8f3582a672c5cbd0074 to your computer and use it in GitHub Desktop.
Save jakubbarczyk/36cc580b1a4ec8f3582a672c5cbd0074 to your computer and use it in GitHub Desktop.
function throttle(callback, fps = 60) {
const frameTime = 1000 / fps;
let lastTime = window.performance.now();
return function run() {
window.requestAnimationFrame(run);
const currentTime = window.performance.now();
const elapsedTime = currentTime - lastTime;
if (elapsedTime < frameTime) return;
else callback();
lastTime = currentTime - elapsedTime % frameTime;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment