Skip to content

Instantly share code, notes, and snippets.

@dpmango
Created March 6, 2017 22:29
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 dpmango/37535462d674e910997442c81f98d3a7 to your computer and use it in GitHub Desktop.
Save dpmango/37535462d674e910997442c81f98d3a7 to your computer and use it in GitHub Desktop.
// set dalay on scroll event to prevent huge memory leaks
(function($) {
var uniqueCntr = 0;
$.fn.scrolled = function (waitTime, fn) {
if (typeof waitTime === "function") {
fn = waitTime;
waitTime = 50;
}
var tag = "scrollTimer" + uniqueCntr++;
this.scroll(function () {
var self = $(this);
var timer = self.data(tag);
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(function () {
self.removeData(tag);
fn.call(self[0]);
}, waitTime);
self.data(tag, timer);
});
}
})(jQuery);
// USAGE
$(window).scrolled(100, function() { // set optional delay time as first param and pass function
// your code here
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment