Skip to content

Instantly share code, notes, and snippets.

@BroFox86
Last active May 30, 2024 11:42
Show Gist options
  • Save BroFox86/8214b9d7aa1a1119ef304011256a612d to your computer and use it in GitHub Desktop.
Save BroFox86/8214b9d7aa1a1119ef304011256a612d to your computer and use it in GitHub Desktop.
[Throttle function] Throttle resize window event #jsTrick
function listenWindowResize() {
let width = window.innerWidth;
let allowReset = true;
window.addEventListener("resize", () => {
if (width === window.innerWidth) {
return;
}
width = window.innerWidth;
if (!allowReset) {
return;
}
allowReset = false;
setTimeout(() => {
allowReset = true;
}, 1000);
// Code...
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment