Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed the loading thing #42

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions static/assets/js/games.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,55 @@
window.addEventListener("load", (event) => {
const gameContainer = document.getElementById("game-container");
const text = document.getElementById("text");
let loadedImages = 0; // Initialize a counter for loaded images

try {
fetch("/assets/json/load/games.json")
.then((response) => response.json())
.then((games) => {
games.sort((a, b) => a.name.localeCompare(b.name));
games.forEach(function (game, gameNum) {
text.innerText = `Loading games (${gameNum + 1}/${games.length})`;
const totalImages = games.length; // Get total number of images

games.forEach(function (game, gameNum) {
let gameHtml;
if (game.usesProxy) {
gameHtml = `<div class="card" style="padding-top: 5px">
<a onclick="${game.alert ? `alert('${game.alert}'); ` : ""}hire('${game.url}');">
<div class="image-container">
<img loading="lazy" src="${game.image}" style="border-radius: 25px">
<p class="item-name">${game.name}</p>
</div>
</a>
</div>`;
<a onclick="${game.alert ? `alert('${game.alert}'); ` : ""}hire('${game.url}');">
<div class="image-container">
<img loading="lazy" src="${game.image}" style="border-radius: 25px"
onload="handleImageLoad(${totalImages})"> <!-- Call function when image loads -->
<p class="item-name">${game.name}</p>
</div>
</a>
</div>`;
} else {
gameHtml = `<div class="card" style="padding-top: 5px">
<a href="${game.url}" rel="noopener noreferrer" ${game.alert ? `onclick="alert('${game.alert}');"` : ""}>
<div class="image-container">
<img loading="lazy" src="${game.image}" style="border-radius: 25px">
<p class="item-name">${game.name}</p>
</div>
</a>
<div class="image-container">
<img loading="lazy" src="${game.image}" style="border-radius: 25px"
onload="handleImageLoad(${totalImages})"> <!-- Call function when image loads -->
<p class="item-name">${game.name}</p>
</div>
</a>
</div>`;
}
gameContainer.insertAdjacentHTML("beforeend", gameHtml);
});
});

text.style.display = "none";

const searchbar = document.getElementById("searchbar");
if (searchbar)
searchbar.placeholder = `Click here or type to search through our ${games.length} games!`;
} catch (error) {
text.innerHTML = `Error in fetching data<br>${error}`;
console.error(error);
}

// Function to handle image load
function handleImageLoad(totalImages) {
loadedImages++; // Increment loaded images counter
text.innerText = `Loading games (${loadedImages}/${totalImages})`; // Update loading text
}
});
Loading