Beginner Mini-Project • HTML • CSS • JavaScript • ⏱ 40 mins
All Mini-Projects →Weekend Build · JavaScript + SVG
⏳ Focus Pomodoro Timer
Master setInterval, animated SVG progress rings, synthesized Web Audio chimes, and automatic break cycles in one standalone productivity widget.
Last updated: November 2026 · 40 min build
A Pomodoro timer alternates 25-minute deep focus blocks with 5-minute short breaks, awarding a 15-minute long break after every 4 completed sessions. You can build the entire visual ring and alert chime using native SVG geometry and the browser's Web Audio API—zero external libraries or audio files required.
What you will build: Three-mode switcher (Focus, Short Break, Long Break), smooth stroke-dashoffset circular progress bar, live browser tab countdown title, synthesized 3-note chime, and persistent session tracking in localStorage.
Test the Working Timer
Open the full-screen demo in a clean tab to test the SVG countdown ring, mode switching, task input, and synthesized Web Audio completion chime.
📋 Complete Single-File Source Code
Save this as pomodoro-timer.html and open it in any browser. Everything is self-contained.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Focus Pomodoro Timer — Live Demo | MAK UI Studio</title>
<meta name="description" content="Interactive Focus Pomodoro Timer live demo with SVG progress ring, Web Audio chime, session tracking, and localStorage persistence.">
<link href="/favicon.png" rel="icon" type="image/png"/>
<style>
:root {
--bg: #020617;
--surface: #0f172a;
--surface-2: #1e293b;
--border: #334155;
--text: #f8fafc;
--muted: #94a3b8;
--accent: #3b82f6;
--accent-glow: rgba(59, 130, 246, 0.35);
--success: #10b981;
--warning: #f59e0b;
}
body.break-mode {
--accent: #10b981;
--accent-glow: rgba(16, 185, 129, 0.35);
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: 'Inter', system-ui, -apple-system, sans-serif;
background: var(--bg);
color: var(--text);
min-height: 100vh;
display: flex;
flex-direction: column;
}
/* Top Demo Bar */
.demo-topbar {
background: rgba(15, 23, 42, 0.92);
backdrop-filter: blur(10px);
border-bottom: 1px solid #1e293b;
padding: 0.75rem 1.25rem;
display: flex;
justify-content: space-between;
align-items: center;
position: sticky;
top: 0;
z-index: 50;
}
.back-btn {
color: #60a5fa;
text-decoration: none;
font-size: 0.85rem;
font-weight: 600;
display: inline-flex;
align-items: center;
gap: 0.4rem;
padding: 0.4rem 0.75rem;
border-radius: 0.5rem;
background: rgba(59, 130, 246, 0.1);
border: 1px solid rgba(59, 130, 246, 0.25);
transition: all 0.2s;
}
.back-btn:hover {
background: rgba(59, 130, 246, 0.2);
color: #93c5fd;
}
.brand-badge {
font-size: 0.78rem;
font-weight: 700;
color: var(--muted);
letter-spacing: 0.04em;
}
/* Main Container */
.main-wrap {
flex: 1;
display: flex;
align-items: center;
justify-content: center;
padding: 2rem 1rem;
}
.timer-card {
background: var(--surface);
border: 1px solid #1e293b;
border-radius: 1.5rem;
padding: 2rem;
width: 100%;
max-width: 440px;
box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.6);
text-align: center;
}
.mode-tabs {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 0.4rem;
background: var(--bg);
padding: 0.35rem;
border-radius: 0.85rem;
border: 1px solid #1e293b;
margin-bottom: 1.75rem;
}
.mode-btn {
background: transparent;
border: none;
color: var(--muted);
font-size: 0.78rem;
font-weight: 600;
padding: 0.55rem 0.4rem;
border-radius: 0.6rem;
cursor: pointer;
transition: all 0.2s;
font-family: inherit;
}
.mode-btn:hover { color: var(--text); }
.mode-btn.active {
background: var(--surface-2);
color: var(--text);
box-shadow: 0 2px 8px rgba(0,0,0,0.3);
}
/* SVG Ring */
.ring-wrapper {
position: relative;
width: 240px;
height: 240px;
margin: 0 auto 1.75rem;
display: flex;
align-items: center;
justify-content: center;
}
.progress-svg {
transform: rotate(-90deg);
width: 240px;
height: 240px;
}
.ring-bg {
fill: none;
stroke: var(--surface-2);
stroke-width: 10;
}
.ring-progress {
fill: none;
stroke: var(--accent);
stroke-width: 10;
stroke-linecap: round;
stroke-dasharray: 653.45;
stroke-dashoffset: 0;
transition: stroke-dashoffset 0.35s linear, stroke 0.3s ease;
filter: drop-shadow(0 0 8px var(--accent-glow));
}
.time-overlay {
position: absolute;
display: flex;
flex-direction: column;
align-items: center;
}
.time-display {
font-size: 3.35rem;
font-weight: 800;
letter-spacing: -0.03em;
font-variant-numeric: tabular-nums;
line-height: 1;
}
.status-label {
font-size: 0.75rem;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 0.14em;
color: var(--accent);
margin-top: 0.5rem;
}
/* Controls */
.controls {
display: flex;
justify-content: center;
gap: 0.75rem;
margin-bottom: 1.75rem;
}
.btn-primary {
background: var(--accent);
color: #fff;
border: none;
padding: 0.85rem 2rem;
border-radius: 0.85rem;
font-size: 0.95rem;
font-weight: 700;
cursor: pointer;
font-family: inherit;
box-shadow: 0 8px 20px var(--accent-glow);
transition: transform 0.15s, opacity 0.2s;
min-width: 135px;
}
.btn-primary:hover { opacity: 0.92; transform: translateY(-1px); }
.btn-primary:active { transform: translateY(0); }
.btn-secondary {
background: var(--surface-2);
color: var(--text);
border: 1px solid var(--border);
padding: 0.85rem 1.15rem;
border-radius: 0.85rem;
font-size: 0.85rem;
font-weight: 600;
cursor: pointer;
font-family: inherit;
transition: background 0.2s;
}
.btn-secondary:hover { background: #334155; }
/* Task Input & Stats */
.task-box {
margin-bottom: 1.25rem;
text-align: left;
}
.task-box label {
display: block;
font-size: 0.72rem;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 0.08em;
color: var(--muted);
margin-bottom: 0.4rem;
}
.task-input {
width: 100%;
background: var(--bg);
border: 1px solid #1e293b;
color: var(--text);
padding: 0.65rem 0.9rem;
border-radius: 0.65rem;
font-size: 0.85rem;
font-family: inherit;
outline: none;
transition: border-color 0.2s;
}
.task-input:focus { border-color: var(--accent); }
.stats-bar {
display: flex;
justify-content: space-between;
align-items: center;
padding-top: 1rem;
border-top: 1px solid #1e293b;
font-size: 0.8rem;
color: var(--muted);
}
.session-dots {
display: flex;
gap: 0.35rem;
align-items: center;
}
.dot {
width: 10px;
height: 10px;
border-radius: 50%;
background: var(--surface-2);
border: 1px solid var(--border);
}
.dot.done {
background: var(--accent);
border-color: var(--accent);
box-shadow: 0 0 6px var(--accent-glow);
}
.reset-stats {
background: transparent;
border: none;
color: #64748b;
font-size: 0.75rem;
cursor: pointer;
text-decoration: underline;
font-family: inherit;
}
.reset-stats:hover { color: #cbd5e1; }
/* Toast */
.toast {
position: fixed;
bottom: 1.5rem;
right: 1.5rem;
background: #1e293b;
color: #f8fafc;
border: 1px solid #334155;
border-left: 4px solid var(--accent);
padding: 0.85rem 1.15rem;
border-radius: 0.75rem;
font-size: 0.85rem;
font-weight: 600;
box-shadow: 0 12px 28px rgba(0,0,0,0.45);
opacity: 0;
transform: translateY(16px);
pointer-events: none;
transition: opacity 0.25s ease, transform 0.25s ease;
z-index: 9999;
}
.toast.show {
opacity: 1;
transform: translateY(0);
}
</style>
</head>
<body>
<div class="demo-topbar">
<a href="/projects/pomodoro-timer.html" class="back-btn">← Back to Lesson & Source Code</a>
<span class="brand-badge">✨ MAK UI STUDIO • LIVE DEMO</span>
</div>
<main class="main-wrap">
<div class="timer-card">
<div class="mode-tabs" role="tablist">
<button class="mode-btn active" data-mode="focus" data-minutes="25">Focus (25m)</button>
<button class="mode-btn" data-mode="short" data-minutes="5">Short Break (5m)</button>
<button class="mode-btn" data-mode="long" data-minutes="15">Long Break (15m)</button>
</div>
<div class="ring-wrapper">
<svg class="progress-svg" viewBox="0 0 240 240">
<circle class="ring-bg" cx="120" cy="120" r="104"></circle>
<circle class="ring-progress" id="progressRing" cx="120" cy="120" r="104"></circle>
</svg>
<div class="time-overlay">
<div class="time-display" id="timeDisplay">25:00</div>
<div class="status-label" id="statusLabel">Ready to Focus</div>
</div>
</div>
<div class="controls">
<button class="btn-primary" id="startPauseBtn">Start</button>
<button class="btn-secondary" id="resetBtn" title="Reset Timer">Reset</button>
<button class="btn-secondary" id="skipBtn" title="Skip to Next Phase">Skip ⏭</button>
</div>
<div class="task-box">
<label for="currentTask">Current Focus Task</label>
<input type="text" id="currentTask" class="task-input" placeholder="e.g., Build CSS Grid layout..." maxlength="60">
</div>
<div class="stats-bar">
<div class="session-dots" id="sessionDots">
<span>Sessions:</span>
<span class="dot" data-idx="1"></span>
<span class="dot" data-idx="2"></span>
<span class="dot" data-idx="3"></span>
<span class="dot" data-idx="4"></span>
<strong id="sessionCountText" style="color:#f8fafc;margin-left:4px;">0</strong>
</div>
<button class="reset-stats" id="resetStatsBtn">Reset Count</button>
</div>
</div>
</main>
<div class="toast" id="toast" role="status"></div>
<script>
(function() {
const MODES = {
focus: { minutes: 25, label: 'Focus Session', isBreak: false },
short: { minutes: 5, label: 'Short Break', isBreak: true },
long: { minutes: 15, label: 'Long Break', isBreak: true }
};
const CIRCUMFERENCE = 2 * Math.PI * 104; // ~653.45
let currentMode = 'focus';
let totalSeconds = MODES.focus.minutes * 60;
let remainingSeconds = totalSeconds;
let timerId = null;
let completedSessions = 0;
const timeDisplay = document.getElementById('timeDisplay');
const statusLabel = document.getElementById('statusLabel');
const progressRing = document.getElementById('progressRing');
const startPauseBtn = document.getElementById('startPauseBtn');
const resetBtn = document.getElementById('resetBtn');
const skipBtn = document.getElementById('skipBtn');
const modeBtns = document.querySelectorAll('.mode-btn');
const currentTaskInput = document.getElementById('currentTask');
const sessionCountText = document.getElementById('sessionCountText');
const dots = document.querySelectorAll('.dot');
const resetStatsBtn = document.getElementById('resetStatsBtn');
const toastEl = document.getElementById('toast');
// Load saved state
try {
completedSessions = parseInt(localStorage.getItem('mak_pomo_sessions') || '0', 10) || 0;
currentTaskInput.value = localStorage.getItem('mak_pomo_task') || '';
} catch (e) {}
function showToast(msg) {
toastEl.textContent = msg;
toastEl.classList.remove('show');
void toastEl.offsetWidth;
toastEl.classList.add('show');
clearTimeout(toastEl._t);
toastEl._t = setTimeout(() => toastEl.classList.remove('show'), 3200);
}
// Web Audio API gentle chime (no external MP3 needed)
function playChime() {
try {
const ctx = new (window.AudioContext || window.webkitAudioContext)();
const notes = [523.25, 659.25, 783.99]; // C5, E5, G5
notes.forEach((freq, i) => {
const osc = ctx.createOscillator();
const gain = ctx.createGain();
osc.type = 'sine';
osc.frequency.setValueAtTime(freq, ctx.currentTime + i * 0.14);
gain.gain.setValueAtTime(0.001, ctx.currentTime + i * 0.14);
gain.gain.exponentialRampToValueAtTime(0.18, ctx.currentTime + i * 0.14 + 0.03);
gain.gain.exponentialRampToValueAtTime(0.0001, ctx.currentTime + i * 0.14 + 0.55);
osc.connect(gain);
gain.connect(ctx.destination);
osc.start(ctx.currentTime + i * 0.14);
osc.stop(ctx.currentTime + i * 0.14 + 0.6);
});
} catch (e) {}
}
function updateStatsUI() {
sessionCountText.textContent = completedSessions;
const cyclePos = completedSessions % 4;
dots.forEach((dot, idx) => {
if (completedSessions > 0 && cyclePos === 0) {
dot.classList.add('done');
} else {
dot.classList.toggle('done', idx < cyclePos);
}
});
}
function updateDisplay() {
const mins = Math.floor(remainingSeconds / 60);
const secs = remainingSeconds % 60;
const formatted = `${String(mins).padStart(2, '0')}:${String(secs).padStart(2, '0')}`;
timeDisplay.textContent = formatted;
document.title = `${formatted} — ${MODES[currentMode].label} | MAK UI Studio`;
const fraction = remainingSeconds / totalSeconds;
const offset = CIRCUMFERENCE * (1 - fraction);
progressRing.style.strokeDashoffset = offset;
}
function stopTimer() {
if (timerId) {
clearInterval(timerId);
timerId = null;
}
startPauseBtn.textContent = 'Start';
}
function switchMode(mode) {
stopTimer();
currentMode = mode;
totalSeconds = MODES[mode].minutes * 60;
remainingSeconds = totalSeconds;
document.body.classList.toggle('break-mode', MODES[mode].isBreak);
modeBtns.forEach(b => b.classList.toggle('active', b.dataset.mode === mode));
statusLabel.textContent = `Ready • ${MODES[mode].label}`;
updateDisplay();
}
function handleComplete(skipped = false) {
stopTimer();
if (!skipped) playChime();
if (currentMode === 'focus') {
if (!skipped) {
completedSessions += 1;
try { localStorage.setItem('mak_pomo_sessions', String(completedSessions)); } catch (e) {}
updateStatsUI();
}
const nextMode = (completedSessions > 0 && completedSessions % 4 === 0) ? 'long' : 'short';
showToast(skipped ? 'Skipped to break' : '🎉 Focus session complete! Time for a break.');
switchMode(nextMode);
} else {
showToast(skipped ? 'Skipped to focus' : '⚡ Break over! Ready to focus.');
switchMode('focus');
}
}
function toggleStartPause() {
if (timerId) {
stopTimer();
statusLabel.textContent = 'Paused';
return;
}
startPauseBtn.textContent = 'Pause';
statusLabel.textContent = MODES[currentMode].label;
timerId = setInterval(() => {
remainingSeconds -= 1;
updateDisplay();
if (remainingSeconds <= 0) {
handleComplete(false);
}
}, 1000);
}
startPauseBtn.addEventListener('click', toggleStartPause);
resetBtn.addEventListener('click', () => {
switchMode(currentMode);
showToast('Timer reset');
});
skipBtn.addEventListener('click', () => handleComplete(true));
modeBtns.forEach(btn => {
btn.addEventListener('click', () => switchMode(btn.dataset.mode));
});
currentTaskInput.addEventListener('input', () => {
try { localStorage.setItem('mak_pomo_task', currentTaskInput.value); } catch (e) {}
});
resetStatsBtn.addEventListener('click', () => {
completedSessions = 0;
try { localStorage.removeItem('mak_pomo_sessions'); } catch (e) {}
updateStatsUI();
showToast('Session counter cleared');
});
progressRing.style.strokeDasharray = CIRCUMFERENCE;
updateStatsUI();
updateDisplay();
})();
</script>
</body>
</html>
How the timer works (6 steps)
An SVG <circle> with radius r="104" has a circumference of 2 × π × 104 ≈ 653.45px. Setting stroke-dasharray: 653.45 creates a single dash covering the entire ring, rotated -90deg so countdowns start cleanly at 12 o'clock.
On every tick, we calculate fraction = remainingSeconds / totalSeconds and set strokeDashoffset = CIRCUMFERENCE * (1 - fraction). CSS transitions smooth out each 1-second step.
A single timerId variable tracks the active setInterval. Clicking Pause, Reset, or switching tabs immediately calls clearInterval(timerId) and sets timerId = null so multiple timers can never stack up.
Instead of loading external .mp3 assets that might fail offline, playChime() uses AudioContext and three sine-wave oscillators (C5, E5, G5) with exponential gain ramps to produce a gentle notification bell.
When a focus block finishes naturally, completedSessions increments and saves to localStorage. Using modulo arithmetic (completedSessions % 4 === 0), every fourth focus session automatically routes into a 15-minute long break.
Updating document.title on every second lets users monitor remaining focus time directly from their browser tab while coding in another window.
🚀 Weekend Extension Challenges
Add a small settings popover allowing users to customize Focus (e.g., 50m) and Break (10m) lengths and persist them in localStorage.
Listen for Spacebar (when the task input isn't focused) to start/pause the countdown, and R to reset.
Store endTime = Date.now() + remainingSeconds * 1000 on Start and calculate remaining time from endTime - Date.now() so background tab throttling never slows the clock.
❓ Frequently asked questions
How do you animate an SVG circular progress ring?
Set stroke-dasharray equal to the circle's circumference (2 * Math.PI * radius) and update stroke-dashoffset proportionally to remaining time.
Why use Web Audio API instead of an MP3 file?
Web Audio synthesizes a clean chime directly in the browser with zero external audio files, zero network requests, and no broken file paths.
How do I prevent duplicate timers when clicking Start twice?
Always guard your start function by checking if an interval ID is already active and clear it properly on pause or reset.
Does setInterval drift over long countdowns?
Yes, browser tab throttling can delay setInterval ticks slightly. For high-precision timers, compare Date.now() timestamps against a target end time.