<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HIIT Timer</title>
<style>
body {
font-family: sans-serif;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #222; /* Dark background for timer */
color: white;
transition: background-color 0.5s ease; /* Smooth color change */
overflow: hidden; /* Prevent scrollbars */
}
.timer-container {
text-align: center;
padding: 30px;
background-color: rgba(0, 0, 0, 0.3); /* Semi-transparent overlay */
border-radius: 15px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.5);
}
#timer-title {
font-size: 2em;
margin-bottom: 15px;
color: #ddd;
}
#current-interval-name {
font-size: 3em;
margin-bottom: 10px;
font-weight: bold;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}
#time-remaining {
font-size: 8em;
font-weight: bold;
margin-bottom: 20px;
text-shadow: 3px 3px 6px rgba(0, 0, 0, 0.4);
}
#set-info {
font-size: 1.5em;
color: #ccc;
}
#completion-message {
font-size: 3em;
font-weight: bold;
color: #4CAF50; /* Green for finished */
display: none; /* Hidden initially */
}
#back-button {
margin-top: 30px;
padding: 10px 20px;
background-color: #555;
color: white;
border: none;
border-radius: 4px;
font-size: 1em;
cursor: pointer;
transition: background-color 0.3s ease;
}
#back-button:hover {
background-color: #777;
}
</style>
<audio id="beep-sound" src="https://www.soundjay.com/buttons/beep-07.wav" preload="auto"></audio>
<audio id="finish-sound" src="https://www.soundjay.com/human/applause-2.wav" preload="auto"></audio>
</head>
<body>
<div class="timer-container">
<div id="timer-title">Loading...</div>
<div id="current-interval-name"></div>
<div id="time-remaining"></div>
<div id="set-info"></div>
<div id="completion-message">Workout Complete!</div>
<button id="back-button" onclick="window.location.href='index.html'">Configure New Timer</button>
</div>
<script>
// Get URL parameters
const urlParams = new URLSearchParams(window.location.search);
// Extract configuration
const timerName = urlParams.get('timerName') || 'Unnamed Timer';
const numSets = parseInt(urlParams.get('numSets') || '1');
const warmup = {
name: urlParams.get('warmupName') || 'Warmup',
duration: parseInt(urlParams.get('warmupDuration') || '0'),
color: urlParams.get('warmupColor') || '#ffdd57'
};
const high = {
name: urlParams.get('highName') || 'Work',
duration: parseInt(urlParams.get('highDuration') || '45'),
color: urlParams.get('highColor') || '#ff6b6b'
};
const low = {
name: urlParams.get('lowName') || 'Rest',
duration: parseInt(urlParams.get('lowDuration') || '15'),
color: urlParams.get('lowColor') || '#6bccf4'
};
const cooldown = {
name: urlParams.get('cooldownName') || 'Cooldown',
duration: parseInt(urlParams.get('cooldownDuration') || '0'),
color: urlParams.get('cooldownColor') || '#70e000'
};
// Build the timer sequence
const sequence = [];
// Add Warmup (if duration > 0)
if (warmup.duration > 0) {
sequence.push(warmup);
}
// Add Sets (High/Low pairs)
for (let i = 0; i < numSets; i++) {
if (high.duration > 0) {
sequence.push(high);
}
// Only add low interval if it's not the *very* last interval of the workout
// This prevents a rest after the last work interval if cooldown is 0 or less
const isLastSet = (i === numSets - 1);
const hasCooldown = cooldown.duration > 0;
if (low.duration > 0 && (!isLastSet || hasCooldown)) {
sequence.push(low);
} else if (low.duration > 0 && isLastSet && !hasCooldown && high.duration > 0) {
// If it's the last set, no cooldown, and we just added High, we still need the final Low
sequence.push(low);
}
}
// Add Cooldown (if duration > 0)
if (cooldown.duration > 0) {
sequence.push(cooldown);
}
// Handle edge case: If sequence is empty (e.g., all durations were 0 or numSets 0)
if (sequence.length === 0) {
document.getElementById('timer-title').textContent = timerName;
document.getElementById('current-interval-name').textContent = 'No Intervals Configured';
document.getElementById('time-remaining').textContent = '--';
document.getElementById('set-info').textContent = '';
document.getElementById('completion-message').textContent = 'Configuration Empty';
document.getElementById('completion-message').style.display = 'block';
document.body.style.backgroundColor = '#555';
return; // Stop execution
}
// Timer state variables
let currentStepIndex = 0;
let timeRemaining = sequence[currentStepIndex].duration;
let timerInterval;
let currentSetDisplay = 1; // Used for displaying current set number
// Get DOM elements
const timerTitleEl = document.getElementById('timer-title');
const intervalNameEl = document.getElementById('current-interval-name');
const timeRemainingEl = document.getElementById('time-remaining');
const setInfoEl = document.getElementById('set-info');
const completionMessageEl = document.getElementById('completion-message');
const bodyEl = document.body;
const beepSound = document.getElementById('beep-sound');
const finishSound = document.getElementById('finish-sound');
// Function to update the display
function updateDisplay() {
const currentInterval = sequence[currentStepIndex];
timerTitleEl.textContent = timerName;
intervalNameEl.textContent = currentInterval.name;
timeRemainingEl.textContent = timeRemaining;
bodyEl.style.backgroundColor = currentInterval.color;
// Calculate which set number to display based on the sequence index
// This is a bit complex because Warmup/Cooldown are outside sets.
// A High/Low pair is one cycle within a set.
let completedSets = 0;
let stepsInSets = 0; // Counter for steps *within* the sets (High/Low)
// Find the index where the first High interval starts (after warmup)
let firstSetStartIndex = sequence.findIndex(step => step === high);
if (firstSetStartIndex !== -1) {
for (let i = firstSetStartIndex; i < currentStepIndex; i++) {
if (sequence[i] === high || sequence[i] === low) {
stepsInSets++;
}
}
// Each completed High/Low pair represents a completed set cycle
// The set display should increment when you *start* the High interval of the next set.
// Or when you are in the Low interval of a set.
// Let's simplify: Set X of Y is displayed when you are in the High or Low of set X.
// The set number is stepsInSets / 2, rounded up, but capped at numSets.
currentSetDisplay = Math.min(numSets, Math.ceil(stepsInSets / ( (high.duration > 0 && low.duration > 0) ? 2 : 1) ) + (firstSetStartIndex === -1 ? 1 : 0) ); // Adjust if only one interval type in set
// More robust set display logic:
let currentSetCounter = 0;
let inSetPhase = false;
for(let i = 0; i <= currentStepIndex; i++){
if(sequence[i] === high){
if(!inSetPhase) { // Just entered set phase
currentSetCounter++;
inSetPhase = true;
}
} else if (sequence[i] === low) {
inSetPhase = true;
} else {
inSetPhase = false; // Exited set phase (Warmup/Cooldown)
}
}
// If currently in Warmup/Cooldown, don't show set info unless it's the end and numSets > 0
if (currentInterval === warmup || currentInterval === cooldown) {
setInfoEl.textContent = ''; // Clear set info during warmup/cooldown
} else {
// If we somehow didn't enter a set phase but are in a high/low (shouldn't happen with loop logic)
if(currentSetCounter === 0 && (currentInterval === high || currentInterval === low)) {
currentSetCounter = 1; // Assume Set 1
}
setInfoEl.textContent = `Set ${Math.max(1, currentSetCounter)} of ${numSets}`;
}
} else {
// No High/Low intervals
setInfoEl.textContent = '';
}
// Check if we are exactly at 0 seconds
if (timeRemaining === 0 && currentStepIndex < sequence.length - 1) {
playBeep();
} else if (timeRemaining === 0 && currentStepIndex === sequence.length - 1) {
playFinishSound();
}
}
// Function to move to the next step
function nextStep() {
currentStepIndex++;
if (currentStepIndex < sequence.length) {
timeRemaining = sequence[currentStepIndex].duration;
updateDisplay(); // Update immediately for the new step
} else {
// Timer finished
clearInterval(timerInterval);
intervalNameEl.textContent = '';
timeRemainingEl.textContent = '';
setInfoEl.textContent = '';
completionMessageEl.style.display = 'block';
bodyEl.style.backgroundColor = '#4CAF50'; // Green background for finish
timerTitleEl.style.display = 'none'; // Hide timer title
}
}
// Timer logic
function timerTick() {
timeRemaining--;
updateDisplay(); // Update every second
if (timeRemaining < 0) {
nextStep(); // Move to next interval
}
}
// Audio functions
function playBeep() {
if (beepSound) {
beepSound.currentTime = 0; // Rewind to start
beepSound.play().catch(e => console.error("Beep sound playback failed:", e));
}
}
function playFinishSound() {
if (finishSound) {
finishSound.currentTime = 0; // Rewind to start
finishSound.play().catch(e => console.error("Finish sound playback failed:", e));
}
}
// Initial display update and start timer
if (sequence.length > 0) {
updateDisplay(); // Show first interval immediately
timerInterval = setInterval(timerTick, 1000); // Start the timer
}
</script>
</body>
</html>