s-yadav / angulargrid

Pinterest like responsive masonry grid system for angular
MIT License
277 stars 105 forks source link

Scroll event handler not removed properly #118

Open gasfab999 opened 7 years ago

gasfab999 commented 7 years ago

In our angular application we have different states/pages. In one of them we have the angular grid. However we noticed an unwanted behavior of it when someone is switching fast between them.

The problem is that

if (scrollNs.scrollContInfo) scrollNs.scrollContInfo.$elm.off('scroll', scrollHandler);

is not removing the scrollHandler as it is not yet set... it is not yet set because the assignment of the event is happening in a timeout:

setTimeout(function() {
    scrollNs.scrollContInfo = getScrollContainerInfo();
    scrollNs.scrollContInfo.$elm.on('scroll', scrollHandler);
}, 0);

This problem could be solved if the timeout would be assigned to a variable (like the infiniteScrollTimeout timeout):

scrollNs.infiniteScrollContainerTimeout = setTimeout(function() {
    scrollNs.scrollContInfo = getScrollContainerInfo();
    scrollNs.scrollContInfo.$elm.on('scroll', scrollHandler);
}, 0);

and then also canceled when the scope is destroyed:

clearTimeout(scrollNs.infiniteScrollContainerTimeout);