Last active
August 29, 2015 13:56
-
-
Save gabssnake/8821233 to your computer and use it in GitHub Desktop.
js throttle debounce scroll resize
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
// 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