(function ($) { 'use strict'; jQuery(document).ready(function () { let $this = jQuery('.popular-post-widget .ajax-widget'); let pageNumber = 2; let total = $this.data('max-pages'); let hasInfinite = $this.data('scroll'); let parentType = $this.data('query-type'); let postType = $this.data('post-type'); let dataValue = $this.data('value'); let ppp = $this.data('ppp'); if (hasInfinite) { let canBeLoaded = true, // this param allows to initiate the AJAX call only if necessary bottomOffset = 2400; // the distance (in px) from the page bottom when you want to load more posts jQuery(window).scroll(function () { if (jQuery(document).scrollTop() > (jQuery(document).height() - bottomOffset) && canBeLoaded === true) { if (pageNumber > total) { return false; } else { let data = {}; data.action = 'ptx_frontend_action'; data.task = 'loadMoreWidget'; data.page_no = pageNumber; data.query_type = parentType; data.post_type = postType; data.value = dataValue; data.ppp = ppp; data.security = ptxFrontendObject._nonce; jQuery.ajax({ url: ptxFrontendObject.ajax_url, type: 'POST', data: data, beforeSend: function (xhr) { $this.append('
'); canBeLoaded = false; }, success: function (html) { $this.append(html); jQuery('.infinite-loader').remove(); canBeLoaded = true; }, complete: function () { cleanTags(); } }); } pageNumber++; } }); } }); })(jQuery);