Created
January 8, 2018 08:23
-
-
Save yayMark/ba30873f641d56875406bd5afc15233e to your computer and use it in GitHub Desktop.
Run code on window resize, but in a way that won't max out CPU.
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
window.addEventListener("resize", resizeThrottler, false); | |
var resizeTimeout; | |
function resizeThrottler() { | |
// ignore resize events as long as an actualResizeHandler execution is in the queue | |
if ( !resizeTimeout ) { | |
resizeTimeout = setTimeout(function() { | |
resizeTimeout = null; | |
actualResizeHandler(); | |
// The actualResizeHandler will execute at a rate of 15fps | |
}, 1000/15); | |
} | |
} | |
function actualResizeHandler() { | |
// code goes here | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://developer.mozilla.org/en-US/docs/Web/Events/resize