关闭滚动监听器

Turning off scroll listener

本文关键字:监听器 滚动      更新时间:2023-09-26
p.initInfiniteScroll = function(){
    $(window).on('scroll', this.infiniteScroll.bind(this));
};
p.terminateInfiniteScroll = function(){
    $(window).off('scroll', this.infiniteScroll);
};
p.infiniteScroll = function(){
    if($(window).scrollTop() == $(document).height() - $(window).height()){
        this.loadMore();
    }
};
p.loadMore = function(){
    console.log('load more');
    this.terminateInfiniteScroll();
};

我init我的无限滚动,然后在滚动时我调用loadMore,在这个函数内,我试图关闭无限滚动,但这没有效果-我在哪里出错?

你需要保持你的"绑定"函数的引用

var myFunc = this.infiniteScroll.bind(this);
p.initInfiniteScroll = function(){
    $(window).on('scroll', myFunc);
};
p.terminateInfiniteScroll = function(){
    $(window).off('scroll', myFunc);
};