Add checks for container presence in animation functions to prevent errors when elements are removed from the DOM [skip ci]

This commit is contained in:
CodeDevMLH
2026-02-27 04:07:29 +01:00
parent 3c1bd01373
commit bd8088c52b
10 changed files with 20 additions and 15 deletions

View File

@@ -245,6 +245,7 @@ function animateRabbit(rabbit) {
isAnimating = false;
rabbitTimeout = setTimeout(() => {
if (!document.body.contains(rabbit)) return;
animateRabbit(document.querySelector('#rabbit'));
}, restTime);
return;

View File

@@ -162,6 +162,7 @@ function startFireworks() {
}
fireworksInterval = setInterval(() => {
if (!document.body.contains(fireworkContainer)) { clearInterval(fireworksInterval); return; }
const randomCount = Math.floor(Math.random() * maxFireworks) + minFireworks;
for (let i = 0; i < randomCount; i++) {
setTimeout(() => {

View File

@@ -61,12 +61,12 @@ function createFriday13(container) {
cat.parentNode.removeChild(cat);
}
// Respawn with random delay between 5 to 25 seconds
setTimeout(spawnCat, Math.random() * 20000 + 5000);
setTimeout(() => { if (document.body.contains(container)) spawnCat(); }, Math.random() * 20000 + 5000);
}, (catWalkDurationSeconds * 1000) + 500); // Wait for duration + 500ms safety margin
}
// Initial spawn with random delay
setTimeout(spawnCat, Math.random() * 5000);
setTimeout(() => { if (document.body.contains(container)) spawnCat(); }, Math.random() * 5000);
}
function initializeFriday13() {

View File

@@ -51,8 +51,9 @@ function createMarioDay(container) {
container.appendChild(wrapper);
// Periodically throw out an 8-bit coin
setInterval(() => {
if (!document.querySelector('.marioday-container')) return;
const intervalId = setInterval(() => {
if (!document.body.contains(container)) { clearInterval(intervalId); return; }
if (container.style.display === 'none') return;
const coin = document.createElement('div');
coin.className = 'mario-coin';

View File

@@ -136,6 +136,7 @@ function createElements() {
for(let i=0; i<maxTrails; i++) trails.push(new Trail());
function loop() {
if (!document.body.contains(container)) { clearInterval(window.matrixInterval); return; }
if (isHidden) return; // Pause drawing when hidden
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.font = 'bold ' + fontSize + 'px monospace';

View File

@@ -56,9 +56,9 @@ function createOscar(container) {
container.appendChild(carpet);
container.appendChild(spotlights);
// Paparazzi flashes with randomized intervals
function flashLoop() {
if (!document.querySelector('.oscar-container')) {
if (!document.body.contains(container)) return; // Kill the loop if container is removed
if (container.style.display === 'none') {
setTimeout(flashLoop, 1000); // Check again later if hidden
return;
}

View File

@@ -238,7 +238,7 @@ function animateSanta() {
function startAnimation() {
const santaHeight = santa.offsetHeight;
if (santaHeight === 0) {
setTimeout(startAnimation, 100);
setTimeout(() => { if (document.body.contains(santa)) startAnimation(); }, 100);
return;
}
// console.log('Santa height: ', santaHeight);
@@ -283,7 +283,7 @@ function animateSanta() {
animationFrameIdSanta = requestAnimationFrame(move);
} else {
const pause = Math.random() * ((maxSantaRestTime - minSantaRestTime) * 1000) + minSantaRestTime * 1000;
setTimeout(animateSanta, pause);
setTimeout(() => { if (document.body.contains(santa)) animateSanta(); }, pause);
}
}

View File

@@ -252,7 +252,8 @@ function createSpace() {
// Swap to a random image from the pool every time it completes an orbit (disappears)
if (imageArr.length > 1) {
// The animation delay pushes the initial cycle, so we use setInterval matched to duration
setInterval(() => {
const intervalId = setInterval(() => {
if (!document.body.contains(container)) { clearInterval(intervalId); return; }
// Update only if currently out of bounds to avoid popping
const rect = symbol.getBoundingClientRect();
if (rect.right < 0 || rect.left > window.innerWidth) {

View File

@@ -203,12 +203,12 @@ function createSports() {
}, arcDuration * 1000 + 500);
// Schedule the next trophy
setTimeout(launchTrophy, Math.random() * 20000 + 10000); // Wait 10-30s until next trophy
setTimeout(() => { if (document.body.contains(container)) launchTrophy(); }, Math.random() * 20000 + 10000); // Wait 10-30s until next trophy
}
// Launch initial trophy after a short delay
if (enableTrophy) {
setTimeout(launchTrophy, Math.random() * 5000 + 2000);
setTimeout(() => { if (document.body.contains(container)) launchTrophy(); }, Math.random() * 5000 + 2000);
}
// Add Germany Colored confetti (Black, Red, Gold)

View File

@@ -295,7 +295,7 @@ function createBird(container) {
wrapper.addEventListener('animationend', (e) => {
if (e.animationName.includes('fly-')) {
wrapper.remove();
createBird(container);
if (document.body.contains(container)) createBird(container);
}
});
@@ -338,7 +338,7 @@ function createButterfly(container) {
wrapper.addEventListener('animationend', (e) => {
if (e.animationName.includes('fly-')) {
wrapper.remove();
createButterfly(container);
if (document.body.contains(container)) createButterfly(container);
}
});
@@ -385,7 +385,7 @@ function createBee(container) {
wrapper.addEventListener('animationend', (e) => {
if (e.animationName.includes('fly-')) {
wrapper.remove();
createBee(container);
if (document.body.contains(container)) createBee(container);
}
});
@@ -428,7 +428,7 @@ function createLadybugGif(container) {
wrapper.addEventListener('animationend', (e) => {
if (e.animationName.includes('walk-')) {
wrapper.remove();
createLadybugGif(container);
if (document.body.contains(container)) createLadybugGif(container);
}
});