metafizzy / infinite-scroll

📜 Automatically add next page
https://infinite-scroll.com
7.41k stars 1.74k forks source link

Proper way to stop scroll requests when scrollThreshold is not reached #934

Closed PhilCliv closed 3 years ago

PhilCliv commented 3 years ago

I have pages where the content downloaded will not reach the scrollThreshold height. In this case, it keeps incrementing the pages and runs many queries per second to the server. I've found a way to stop this but it is raising an error "Uncaught TypeError: Cannot read property 'querySelectorAll' of null" Is there a proper way to do it? Thank you! Here is my code:

infiniteScroll = $(target).infiniteScroll({
        path: someUrl,
        append: '.grid-item',
        history: false,
        outlayer: msnry,
        scrollThreshold: 1000,
        prefill: true
    });

    infiniteScroll.on('load.infiniteScroll', function (event, response) {
        if (!response || !Object.keys(response).length) {
            // disable scroll load behavior if no response
           infiniteScroll.data('infiniteScroll').scrollThreshold = false;
            return false;
        }
    });
desandro commented 3 years ago

Infinite Scroll v4 was just released earlier this week with a fix for this kind of case. Can you confirm you are using v4.0.x?

PhilCliv commented 3 years ago

You are right. With the version 4 it's working perfect. Thank you!