Skip to content

Instantly share code, notes, and snippets.

@richarddewit
Last active April 18, 2018 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richarddewit/af121d6bd118f833f09de33c5e203542 to your computer and use it in GitHub Desktop.
Save richarddewit/af121d6bd118f833f09de33c5e203542 to your computer and use it in GitHub Desktop.
Throttled resize/scroll events
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;
}
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