please tell me how not to execute ajax request if respond empty?

var startFrom = 2; var count = 0; $(window).scroll(function() { if( $(window).scrollTop() + $(window).height() >= $(document).height() ){ count++; $.ajax({ url: '/index.php/posts/index/products', method: 'POST', dataType: 'json', data: {"startFrom" : startFrom, "count" : count, "categoryId": <?php echo $categoryId ?> }, success: function(response){ //console.log(response); if (response!="") { $('.products-grid').append(response); startFrom++; }else{ $('.products-grid').append(response); } } }); } }); 
  • 2
    if respond - meaning the result of the ajax request, then nothing. - Grundy
  • This is the code for ajax scrolling of goods on the site (if any - added), but the fact is that when all the goods are displayed, the scroll will execute the Ajax request, which returns nothing, how to rewrite the code, so that when there are no more goods, the request does not was performed. - Sklerozz
  • you need to check before the request that all have already been withdrawn - Grundy
  • @Grundy, do not tell me in more detail please? - Sklerozz
  • one
    @Grundy rather, if (предыдущий ответ не был пуст) { $.ajax(...) } - nörbörnën

1 answer 1

Just added a flag. inProgress = true, a when response empty flag becomes inProgress = false; And added a check on inProgress to the if( $(window).scrollTop() + $(window).height() >= $(document).height() && inProgress)