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

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -