Last active
April 18, 2018 08:10
-
-
Save richarddewit/af121d6bd118f833f09de33c5e203542 to your computer and use it in GitHub Desktop.
Throttled resize/scroll events
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
var resizing = false; | |
$(window).on('resize', function() { | |
if (!resizing) { | |
resizing = true; | |
if (!window.requestAnimationFrame) { | |
setTimeout(onResizeFunction, 250); | |
} else { | |
requestAnimationFrame(onResizeFunction); | |
} | |
} | |
}); | |
function onResizeFunction() { | |
// ... do someting on resize ... | |
resizing = false; | |
} |
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
var scrolling = false; | |
$(window).on('scroll', function() { | |
if (!scrolling) { | |
scrolling = true; | |
if (!window.requestAnimationFrame) { | |
setTimeout(onScrollFunction, 250); | |
} else { | |
requestAnimationFrame(onScrollFunction); | |
} | |
} | |
}); | |
function onScrollFunction() { | |
// ... do someting on scroll ... | |
scrolling = false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment