为什么我的Scroll事件在这里被调用两次?

时间:2016-10-28 19:05:20

标签: javascript angularjs javascript-events addeventlistener event-listener

enter image description here

InfiniteScrollFactory:

SQL Error [102][S0001]:Incorrect syntax near '='.

当我向下滚动鼠标一次时,我的const scrollingSocial = (e) => { console.log('scrollingSocial'); // e.stopPropagation(); const reachedBottom = () => socialCol.scrollHeight - socialCol.scrollTop === socialCol.offsetHeight; const loadMoreItems = () => { console.log('[ Fire Once ] loadMoreItems...'); $rootScope.$emit("socialmedia.stream.load"); }; if (reachedBottom()) loadMoreItems(); }; const wireSocialScroll = (list) => { console.log('wireSocialScroll called!'); if (notEmpty(list)) { socialCol.addEventListener('scroll', scrollingSocial); } }; const attachScrollListener = (location, col, list) => { console.log('attachScrollListener'); console.log(' location', location); switch (location) { // case 'tagsPanel' : tagsCol = col; wireTagsScroll(list); break; // case 'feedPanel' : feedCol = col; wireFeedScroll(list); break; case 'socialMedia' : socialCol = col; wireSocialScroll(list); break; } }; 函数被调用一次。它需要大约45个“滚动”才能最终触发我的scrollingSocial功能。然而,它被调用两次。即使我第46次没有滚动,我也看到滚动第46次。

socialMediaDirective:

loadMoreItems

1 个答案:

答案 0 :(得分:1)

滚动和它的事件触发器可能有点挑剔。

只需使用此代码:

$(document).on('scroll', () => console.log('scroll'));

每次我勾选鼠标滚轮时都会得到多个滚动条,无论我多么小心地这样做。

这可能与你所拥有的问题相同。你要做的只是添加一个布尔值来跟踪你是否调用了loadMoreItems,使用该布尔值来阻止它再次调用它。

let loadingMoreItems = false;
const scrollingSocial = (e) => {
    console.log('scrollingSocial');
    // e.stopPropagation();
    const reachedBottom = () => socialCol.scrollHeight - socialCol.scrollTop === socialCol.offsetHeight;
    const loadMoreItems = () => {
        console.log('[ Fire Once ] loadMoreItems...');
        $rootScope.$emit("socialmedia.stream.load");
    };
    if (!loadingMoreItems && reachedBottom()) {
        loadingMoreItems = true;
        loadMoreItems();
    }
};

然后,在适当的时间(或多次),将该布尔值更改为false以允许其再次调用(向上滚动,加载更多项目,reachedBottom()导致错误一次等)。