This commit is contained in:
partisan 2024-12-02 21:32:30 +01:00
parent 96b92fed5e
commit a17deb4af1
6 changed files with 27 additions and 87 deletions

View file

@ -2,6 +2,8 @@
// Configuration
const imageStatusInterval = 500; // Interval in milliseconds to check image status
const scrollThreshold = 500; // Distance from bottom of the page to trigger loading
const loadingIndicator = document.getElementById('message-bottom-left');
let loadingTimer;
let isFetching = false;
let page = parseInt(document.getElementById('template-data').getAttribute('data-page')) || 1;
let query = document.getElementById('template-data').getAttribute('data-query');
@ -51,17 +53,25 @@
*/
function fetchNextPage() {
if (isFetching || noMoreImages) return;
// Start the timer for loading indicator
loadingTimer = setTimeout(() => {
loadingIndicator.style.display = 'flex';
}, 150);
isFetching = true;
page += 1;
fetch(`/search?q=${encodeURIComponent(query)}&t=image&p=${page}&ajax=true`)
.then(response => response.text())
.then(html => {
// Parse the returned HTML and extract image elements
clearTimeout(loadingTimer); // Clear the timer if fetch is successful
loadingIndicator.style.display = 'none'; // Hide the loading indicator
let parser = new DOMParser();
let doc = parser.parseFromString(html, 'text/html');
let newImages = doc.querySelectorAll('.image');
if (newImages.length > 0) {
let resultsContainer = document.querySelector('.images');
newImages.forEach(imageDiv => {
@ -77,9 +87,9 @@
img.onerror = function() {
handleImageError(img);
};
let id = img.getAttribute('data-id');
if (id) { // Only include if ID is not empty
if (id) {
imageElements.push(img);
imageIds.push(id);
}
@ -98,6 +108,8 @@
isFetching = false;
})
.catch(error => {
clearTimeout(loadingTimer); // Clear the timer if fetch fails
loadingIndicator.style.display = 'none'; // Hide the loading indicator
console.error('Error fetching next page:', error);
isFetching = false;
});