Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vuongtran/67ca65d2b7b81a58fa01 to your computer and use it in GitHub Desktop.
Save vuongtran/67ca65d2b7b81a58fa01 to your computer and use it in GitHub Desktop.
Directly attaching a handler to the scroll event is considered bad practice because (depending on the browser) this event can fire many times within a few milliseconds. A solution below will be deal with this issue.
var scrollticker;
jQuery(window).scroll(function() {
if (scrollticker) {
window.clearTimeout(scrollticker);
scrollticker = null;
}
// Set Timeout
scrollticker = window.setTimeout(function() {
if (jQuery('#div-fetching').length) {
if (jQuery(window).scrollTop() >= jQuery('#div-fetching').offset().top + jQuery('#div-fetching').outerHeight() - window.innerHeight) {
//code fetching handle matrix
}
}
}, 500); // Timeout in msec
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment