Skip to content

Instantly share code, notes, and snippets.

@Samjin
Last active July 5, 2016 06:34
Show Gist options
  • Save Samjin/aea494432e09eabdb839 to your computer and use it in GitHub Desktop.
Save Samjin/aea494432e09eabdb839 to your computer and use it in GitHub Desktop.
Scroll() event optimization
var throttle = (function () {
return function (fn, delay) {
delay || (delay = 100);
var last = +new Date;
return function () {
var now = +new Date;
if (now - last > delay) {
fn.apply(this, arguments);
last = now;
}
};
};
})();
And then it can be used like this:
var outerPane = $details.find(".details-pane-outer"),
$(window).scroll(throttle(function() {
// Check your page position and then
// Load in more results
}, 250));
https://github.com/shichuan/javascript-patterns/blob/master/jquery-patterns/window-scroll-event.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment