javascript - Window scroll event fires multiple times -
i working marionette.js app fetches more data user scrolls down page.
setupwindowscrolllistener: function() { var $window = $(window), $document = $(document), = this; $window.on('scroll', _.debounce(function() { var scrolltop = $window.scrolltop(), wheight = $window.height(), dheight = $document.height(), margin = 100; if(scrolltop + wheight > dheight - margin) { eventer.trigger('fetch:more:products'); } }, 500)); }
the problem when user scrolls down bottom of page, eventer
firing off multiple events instead of one.
update
code isloading check
setupwindowscrolllistener: function() { var $window = $(window), $document = $(document), = this, isloading = false; $window.on('scroll', _.throttle(function() { if(!isloading) { var scrolltop = $window.scrolltop(), wheight = $window.height(), dheight = $document.height(), margin = 100; if(scrolltop + wheight > dheight - margin) { isloading = true; eventer.trigger('fetch:more:products'); isloading = false; } } }, 500)); }
Comments
Post a Comment