Skip to content

Instantly share code, notes, and snippets.

@gabssnake
Last active August 29, 2015 13:56
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 gabssnake/8821233 to your computer and use it in GitHub Desktop.
Save gabssnake/8821233 to your computer and use it in GitHub Desktop.
js throttle debounce scroll resize
// throttle - http://ejohn.org/blog/learning-from-twitter/
// be sure to cache selector
var elem = $(".details"),
scrolled = false;
$(window).on('scroll', function() {
scrolled = true;
});
// $(window).on('resize', function() { resized = true; });
setInterval(function() {
if (scrolled) {
scrolled = false;
// do stuff with "elem"
}
}, 250);
// ---------------------------------------
// debounce
var timer = null;
$(window).scroll(function() {
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(myCallback, 100);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment