From 35129d29cc6d0138b13652c41ce5ca2490946090 Mon Sep 17 00:00:00 2001 From: CodeDevMLH Date: Mon, 25 Nov 2024 00:35:07 +0100 Subject: [PATCH] show log only once, add random after dom loaded --- snowflakes.js | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/snowflakes.js b/snowflakes.js index 5464415..dadd9a3 100644 --- a/snowflakes.js +++ b/snowflakes.js @@ -2,6 +2,8 @@ const randomSnowflakes = false; // enable random Snowflakes const snowflakeCount = 50; // count of random extra snowflakes +let msgPrinted = false; // flag to prevent multiple console messages + // function to check and control the snowflakes function toggleSnowflakes() { const snowflakeContainer = document.querySelector('.snowflakes'); @@ -14,10 +16,16 @@ function toggleSnowflakes() { // hide snowflakes if video player is active or dashboard is visible if (videoPlayer || isDashboard || hasUserMenu) { snowflakeContainer.style.display = 'none'; // hide snowflakes - console.log('Snowflakes hidden'); + if (!msgPrinted) { + console.log('Snowflakes hidden'); + msgPrinted = true; + } } else { snowflakeContainer.style.display = 'block'; // show snowflakes - console.log('Snowflakes visible'); + if (msgPrinted) { + console.log('Snowflakes visible'); + msgPrinted = false; + } } } @@ -50,20 +58,24 @@ function addRandomSnowflakes(count) { // set random horizontal position, size and animation delay const randomLeft = Math.random() * 100; // position (0% to 100%) //const randomSize = Math.random() * 1.5 + 0.5; // size (0.5em to 2em) - const randomAnimationDelay = Math.random() * 5; // delay (0s to 5s) + const randomAnimationDelay = Math.random() * 8; // delay (0s to 8s) + const randomAnimationDelay2 = Math.random() * 5; // delay (0s to 5s) // apply styles snowflake.style.left = `${randomLeft}%`; //snowflake.style.fontSize = `${randomSize}em`; - snowflake.style.animationDelay = `${randomAnimationDelay}s, ${randomAnimationDelay / 2}s`; + snowflake.style.animationDelay = `${randomAnimationDelay}s, ${randomAnimationDelay2}s`; // add the snowflake to the container snowflakeContainer.appendChild(snowflake); } + console.log('Random snowflakes added'); } // initialize snowflakes -toggleSnowflakes(); //check if snowflakes should be hidden -if (randomSnowflakes) { - addRandomSnowflakes(snowflakeCount); //add random snowflakes -} \ No newline at end of file +document.addEventListener('DOMContentLoaded', () => { + toggleSnowflakes(); + if (randomSnowflakes) { + addRandomSnowflakes(snowflakeCount); + } +}); \ No newline at end of file