javascript - window.removeEventListener with a named function isn't working -
i using react , below code using implement infinite scroll feature.
componentdidmount() { // flag check if content has loaded. let flag = true; function infinitescroll() { let enterprisewrap = $('.enterprise-blocks'); let contentheight = enterprisewrap.offsetheight; let yoffset = window.pageyoffset; let y = yoffset + window.innerheight; console.log('hey'); if(this.props.hasmore) { if(y >= contentheight && flag) { flag = false; this.props.loadmorevendors(function() { flag = true; }); } } else { window.removeeventlistener('scroll', infinitescroll.bind(this)); } } window.addeventlistener('scroll', infinitescroll.bind(this)); }
i wanna unbind scroll event once items loaded removeeventlistener not working. doing wrong?
every time bind function, new function back. removing different listener 1 added initially. store result of function.bind
, use in both places
this.boundinfinitescroll = this.infinitescroll.bind(this); ... } else { window.removeeventlistener('scroll', this.boundinfinitescroll); } } window.addeventlistener('scroll', this.boundinfinitescroll);
Comments
Post a Comment