jQuery(function($){ $('.buzznews-infinite-scrolling-post').append( '' ); var button = $('.buzznews-infinite-scrolling-post .load-more'); var page = 1; var loading = false; var scrollHandling = { allow: true, reallow: function() { scrollHandling.allow = true; }, delay: 400 //(milliseconds) adjust to the highest acceptable value }; $(window).scroll(function(){ if( ! loading && scrollHandling.allow ) { scrollHandling.allow = false; setTimeout(scrollHandling.reallow, scrollHandling.delay); var offset = $(button).offset().top - $(window).scrollTop(); if( 2000 > offset ) { loading = true; var data = { action: 'buzznews_ajax_load_more', nonce: buzznewsLoadMore.nonce, page: page, query: buzznewsLoadMore.query, }; $.post(buzznewsLoadMore.url, data, function(res) { if( res.success) { $('.buzznews-infinite-scrolling-post').append( res.data ); $('.buzznews-infinite-scrolling-post').append( button ); page = page + 1; debugger; loading = false; } else { // console.log(res); } }).fail(function(xhr, textStatus, e) { // console.log(xhr.responseText); }); } } }); });