@markuipress sure!
In this case, I’m using a snippets plugin to load the JS, and I have conditions set to only load this on the Live Traffic page, inline in the admin footer. Not positive if this is the best way to do it, but it works for me.
var observer;
var lastContainer = null; // Store the last detected scroll container
var loadingListings = false; // Flag to track if a request is in progress
function attachScrollEvent() {
var newScrollable = jQuery("#uip-id-of-the-scrollable-container");
// Prevent re-attaching if it's the same container
if (newScrollable.length && newScrollable[0] !== lastContainer) {
lastContainer = newScrollable[0]; // Update last container
console.log("New scroll container detected:", newScrollable);
newScrollable.off("scroll").on("scroll", function () {
var container = jQuery(this);
var containerHeight = container.outerHeight();
var containerScrollTop = container.scrollTop();
var containerScrollBottom = containerScrollTop + containerHeight;
var scrollThreshold = container[0].scrollHeight - containerHeight;
// Add a slight buffer to trigger before exactly reaching the bottom
var buffer = 50; // Adjust as needed
// If no request is in progress and we're near the bottom, trigger the load
if (!loadingListings && containerScrollBottom + buffer >= scrollThreshold) {
console.log("Infinite scroll triggered!");
loadingListings = true; // Set flag to true to prevent multiple requests
WFAD.wfLiveTraffic.loadNextListings(function () {
console.log("Next listings loaded.");
WFAD.reverseLookupIPs();
WFAD.avatarLookup();
// Reset the loading flag after the request completes
loadingListings = false;
});
}
});
}
}
// Debounce function to avoid excessive calls
function debounce(func, delay) {
var timer;
return function () {
clearTimeout(timer);
timer = setTimeout(func, delay);
};
}
// Observe changes and attach the scroll event
observer = new MutationObserver(debounce(attachScrollEvent, 500));
observer.observe(document.body, { childList: true, subtree: true });
// Initial attachment in case the container already exists
attachScrollEvent();