tiny jquery way
$.fn.scrollStopped = function(callback) { var that = this, $this = $(that); $this.scroll(function(ev) { clearTimeout($this.data('scrollTimeout')); $this.data('scrollTimeout', setTimeout(callback.bind(that), 250, ev)); }); };
250 ms after the last scroll event, this will trigger the scrollStopped callback.
http://jsfiddle.net/wtRrV/256/
Lodash (even smaller)
function onScrollStopped(domElement, callback) { domElement.addEventListener('scroll', _.debounce(callback, 250)); }
http://jsfiddle.net/hotw1o2j/
pure JS (technically the smallest)
function onScrollStopped(domElement, callback, timeout = 250) { domElement.addEventListener('scroll', () => { clearTimeout(callback.timeout); callback.timeout = setTimeout(callback, timeout); }); }
https://jsfiddle.net/kpsxdcv8/15/
strange fact
The clearTimeout and clearInterval parameters need not be specified and may even be of the wrong type or even omitted.
http://jsfiddle.net/2w5zLwvx/
Jason Sebring Dec 26 '12 at 1:23 2012-12-26 01:23
source share