var debounce_1; row_processate_1 = []; $(window).bind('scroll', function() { if (debounce_1) clearTimeout(debounce_1); //start a new delay debounce_1 = setTimeout(function(){ //remove reference so another delay can start debounce_1 = null; //perform whatever logic you normally would do var row = Number($('#row').val()); var allcount = Number($('#all').val()); var id_categoria = Number($('#id_categoria').val()); if (isNaN(id_categoria)){ id_categoria = ''; } var rowperpage = Number($('#quante').val()); var rowno = row + rowperpage; if( ($(window).scrollTop() == parseInt(($('.filter-content').offset().top + $('.filter-content').outerHeight() - window.innerHeight)),10) && (rowno <= allcount) && !row_processate_1.includes(rowno)) { row_processate_1.push(rowno); $(".load-more-products").text("Caricamento..."); setTimeout(function () { row = row + rowperpage; if(row <= allcount){ $("#row").val(row); $.ajax({ url: 'https://www.ramellagraniti.com/assets/ajax/getproducts.php', type: 'post', data: {row:row,rowperpage:rowperpage,id_categoria:id_categoria}, beforeSend:function(){ $(".load-more-products").text("Caricamento..."); }, success: function(response){ // Setting little delay while displaying new content setTimeout(function() { console.log("reload 1"); var $newItems = $(response); $(".product-listing").append( $newItems ).isotope('appended', $newItems ); $(".product-listing").imagesLoaded().progress( function() { $(".product-listing").isotope('layout'); }); //.masonry(); //$('.news').masonry('appended', newItems ); var rowno = row + rowperpage; // checking row value is greater than allcount or not if(rowno > allcount){ // Change the text and background $('.load-more-products').hide(); //$('.load-more').css("background","darkorchid"); } }, 100); } }); }else{ $('.load-more-products').text("Caricamento..."); // Setting little delay while removing contents setTimeout(function() { // When row is greater than allcount then remove all class='post' element after 3 element $('.immagine-album:nth-child(3)').nextAll('.post').remove(); // Reset the value of row $("#row").val(0); // Change the text and background $('.load-more').text("VISUALIZZA ALTRI PRODOTTI"); }, 2000); } }, 2000); } }, 300); });