0

I'm working on building an infinite page-scroll, at least until it reaches the last page. However, it scrolls way to quickly and overloads my web-browser. I wanted to know how I can implement a time-lag on each scroll, something like 1-2 seconds between the scrolls.

Here's what I have developed so far:

var infScroll = setInterval(function () {
                                    var scrollingElement = (document.scrollingElement || document.body);
                                    scrollingElement.scrollTop = scrollingElement.scrollHeight;
                                }, 200);

It works and keeps scrolling down the page, however, how do I set a lag between each scroll?

I have tried updating it with a timer as the following:

function disableScroll() {
    // temporarily disable action
    scrollEnabled = false;

    // set a timer to enable again it 1 second from now
    setTimeout(function() {
        scrollEnabled = true;
    }, 8000);
}
var infScroll = setInterval(function () {
                var scrollingElement = (document.scrollingElement || document.body);
                scrollingElement.scrollTop = scrollingElement.scrollHeight; disableScroll()
                                }, 200);

However, the function won't stop the scroll at all for a few moments.

Emil11
  • 199
  • 9
  • 1
    Did you search it on SO? For example, this one: https://stackoverflow.com/a/43704679/3500157 Also if you don't like this one - have look around there. – Alexander B. Feb 28 '22 at 19:37
  • 1
    I don't fully understand the question. If the concern is `scroll` event firing too frequently, you might want to implement a throttle handler as in [this SO answer](https://stackoverflow.com/a/51020071/7216508). – Bumhan Yu Feb 28 '22 at 19:46
  • Thank you both for these suggestions, and yes I've had a look around and couldn't find anything specific. Primarily, I could not find anything simple. I'll see what I can learn from these two links, but I was hoping for a simpler method. – Emil11 Feb 28 '22 at 19:50
  • @AlexanderB. I have updated my code, I'm still learning so please let me know how to integrate the function properly. I have been using something similar to [this SO answer](https://stackoverflow.com/questions/31083649/how-can-i-set-a-delay-for-next-scroll-event) – Emil11 Feb 28 '22 at 23:03
  • you update scrollEnabled, but never do ```if (scrollEnabled)``` inside setInterval. – Alexander B. Feb 28 '22 at 23:10
  • @AlexanderB. right, I've tried integrating the if statement into the body but my loop would override it. I need to somehow set some limit for it to activate. Not sure how to do this – Emil11 Mar 01 '22 at 09:48

0 Answers0