我正在使用React,下面是我用来实现无限滚动功能的代码。带命名函数的window.removeEventListener不起作用
componentDidMount() {
// Flag to check if the 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));
}
我实际上想要解除滚动事件一旦所有的项目都加载,但removeEventListener不工作。难道我做错了什么?
真棒。非常感谢。 –