Last active
May 30, 2024 11:42
-
-
Save BroFox86/8214b9d7aa1a1119ef304011256a612d to your computer and use it in GitHub Desktop.
[Throttle function] Throttle resize window event #jsTrick
This file contains 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 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