Created
March 6, 2017 22:29
-
-
Save dpmango/37535462d674e910997442c81f98d3a7 to your computer and use it in GitHub Desktop.
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
// 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