JavaScript event handling scroll event with a delay

Viewed 25492

Can someone explaine and help me with this. My webpage is slugish because the scroll function is dragging it down. I need to add a delay but don't understand how to do this.

$(window).scroll(handleScroll);
2 Answers

For scroll you most likely need throttle function like in Lodash or Underscore, great example:

function throttle(func, timeFrame) {
  var lastTime = 0;
  return function () {
      var now = new Date();
      if (now - lastTime >= timeFrame) {
          func();
          lastTime = now;
      }
  };
}

This is simple implementation from this repo

Related