Last active
December 25, 2023 14:37
-
-
Save jakubbarczyk/36cc580b1a4ec8f3582a672c5cbd0074 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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