Scroll Function Firing Multiple Times Instead of Once

前端 未结 2 1503
一向
一向 2021-01-06 01:59

I am trying to create a website that automatically scrolls to each section upon a single scroll action. This means that the code has to check if the page is scrolled up or s

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-06 02:58

    The scroll event (as well as the resize event) fire multiple times as a user does this. To help this, the best practice is to debounce. However, I've never gotten that to work. Instead, I use a global boolean to check if the function has fired.

    var scrolled = false;
    page.on('scroll', function(){
        if(!scrolled){
            scrolled = true;
            //do stuff that should take a while...
            scrolled = false;
        };
    });
    

提交回复
热议问题