Skip to content

Instantly share code, notes, and snippets.

@yayMark
Created January 8, 2018 08:23
Show Gist options
  • Save yayMark/ba30873f641d56875406bd5afc15233e to your computer and use it in GitHub Desktop.
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.
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
}
@yayMark
Copy link
Author

yayMark commented Jan 8, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment