Of course! Here is a single JavaScript function that dynamically creates the necessary CSS and HTML, runs the countdown, and then cleans up after itself.
You can paste this code into the <script> tag of any HTML file, and it will work without any pre-existing HTML (besides <body>) or CSS.
The All-in-One JavaScript Function
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dynamic Countdown</title>
</head>
<body>
<button onclick="createAndRunCountdown()" style="font-size: 1.5rem; padding: 1rem; cursor: pointer;">
Start Countdown
</button>
<script>
function createAndRunCountdown() {
// --- 1. Create and Inject CSS ---
// We define all the necessary styles as a string.
const styles = `
/* Style for the container to center the square */
#countdown-container {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
background-color: rgba(0, 0, 0, 0.5); /* Optional: darkens the background */
}
/* The square itself */
#countdown-square {
width: 50vw;
height: 50vh;
background-color: #2c3e50; /* A dark blue color */
display: flex;
justify-content: center;
align-items: center;
color: white;
font-size: 20vmin; /* Use vmin to make font size responsive */
font-weight: bold;
font-family: Arial, sans-serif;
opacity: 0; /* Start invisible for a fade-in effect */
transform: scale(0.7); /* Start slightly smaller */
transition: opacity 0.4s ease-in-out, transform 0.4s ease-in-out;
}
/* The animation for the number flash */
@keyframes flash {
0% { opacity: 0.3; transform: scale(1); }
50% { opacity: 1; transform: scale(1.1); }
100% { opacity: 0.3; transform: scale(1); }
}
.flash-effect {
animation: flash 1s ease-in-out;
}
`;
// Create a <style> element and add our CSS to it
const styleSheet = document.createElement("style");
styleSheet.innerText = styles;
document.head.appendChild(styleSheet);
// --- 2. Create HTML Elements ---
const container = document.createElement('div');
container.id = 'countdown-container';
const square = document.createElement('div');
square.id = 'countdown-square';
// Append the square to the container, and the container to the body
container.appendChild(square);
document.body.appendChild(container);
// --- 3. Run the Countdown Logic ---
let count = 3;
// Use a short timeout to allow the elements to be in the DOM before animating
setTimeout(() => {
// Fade in the square
square.style.opacity = '1';
square.style.transform = 'scale(1)';
const updateNumber = () => {
if (count > 0) {
square.textContent = count;
// Add the flash class, and remove it after the animation ends
square.classList.add('flash-effect');
setTimeout(() => {
square.classList.remove('flash-effect');
}, 1000); // Animation duration is 1s
count--;
}
};
// Initial call for the number '3'
updateNumber();
// Set an interval to update the number every second
const countdownInterval = setInterval(() => {
if (count > 0) {
updateNumber();
} else {
// Countdown finished
clearInterval(countdownInterval);
// Fade out the square
square.style.opacity = '0';
square.style.transform = 'scale(0.7)';
// --- 4. Cleanup ---
// After the fade-out animation is done, remove elements from the DOM
setTimeout(() => {
document.body.removeChild(container);
document.head.removeChild(styleSheet);
}, 500); // Matches the transition duration (0.4s) + a little buffer
}
}, 1000);
}, 50); // A tiny delay to ensure smooth transitions
}
</script>
</body>
</html>
How It Works:
-
Create and Inject CSS:
- A string
styles holds all the CSS needed for the container, the square, and the flashing animation keyframes.
- A
<style> element is created in JavaScript.
- The
styles string is injected into this element, which is then appended to the document's <head>. This makes the styles available globally.
-
Create HTML Elements:
- A
div with the ID countdown-container is created. This will act as an overlay and a flexbox parent to center the square.
- A
div with the ID countdown-square is created.
- The
square is appended inside the container, and the container is appended to the <body>.
-
Run Countdown Logic:
- A
count variable is initialized to 3.
- We use
setInterval to run a piece of code every 1000 milliseconds (1 second).
- Inside the interval, it updates the
textContent of the square and applies a CSS class (flash-effect) to trigger the animation.
- When the count reaches
0, the interval is cleared using clearInterval to stop it from running forever.
-
Cleanup:
- Once the countdown is over, the square is faded out by changing its opacity.
- A
setTimeout is used to wait for the fade-out animation to complete.
- After the wait, both the
container element (with the square inside it) and the <style> element are completely removed from the DOM, leaving the page exactly as it was before the function was called.