infinite-scroll icon indicating copy to clipboard operation
infinite-scroll copied to clipboard

checkLastPage not working when retrieving JSON

Open Snoop9 opened this issue 5 years ago • 2 comments

Hello, I'm using Infinite Scroll with this code :

$container.infiniteScroll({
  path: function() {
    var pageIndex = this.pageIndex + 1;
    var url = jQuery('#posts').data('url');
    url = url+pageIndex;
    return url;
  },
  append: false,
  responseType: 'text',
});

$container.on( 'load.infiniteScroll', function( event, response ) {
  var data = JSON.parse( response );
  $container.append( data.content );
});

But, when "/getLastPost" url is returning a blank JSON, infinite scroll is not detecting that empty result and is still trying to load the next page.

How can I fix this ?

Snoop9 avatar Jan 03 '20 15:01 Snoop9

One solution is to set scrollThreshold: false when there is no more content to append.

$container.on( 'load.infiniteScroll', function( event, response ) {
  if ( !response || !Object.keys( response ).length  ) {
    // disable scroll load behavior if no response
    $container.data('infiniteScroll').scrollThreshold = false;
    return;
  }
  var data = JSON.parse( response );
  $container.append( data.content );
});

desandro avatar Apr 20 '20 16:04 desandro

Event when my path function returns nothing. It still tries to fetch next page with path as "undefined"

page-load.js:55 GET http://localhost:3000/undefined 404 (Not Found)

kabirsaheb avatar May 14 '21 19:05 kabirsaheb