The Pomodoro technique is simple: work for 25 minutes, take a 5 minute break, and after four rounds take a longer one. It's also a great first JavaScript project, because a small timer touches most of the basics: the DOM, events, state, and timing.
In this project we'll build one with plain HTML, CSS and JavaScript. No frameworks, no build step, three files.
What we're building
- A 25 minute focus timer, 5 minute short breaks, and a 15 minute long break after every fourth session
- Start, pause and reset buttons, plus the spacebar to start and pause
- The countdown in the browser tab title, so you can see it from another tab
- A short beep when each block ends
- A count of completed sessions
Step 1: the HTML
Create a folder with index.html, style.css and app.js. The markup is small:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Pomodoro</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<main class="timer">
<p id="mode">Focus</p>
<h1 id="time">25:00</h1>
<div class="controls">
<button id="start">Start</button>
<button id="reset">Reset</button>
</div>
<p id="count">Sessions done: 0</p>
</main>
<script src="app.js"></script>
</body>
</html>
Step 2: a little CSS
Just enough to centre everything and make the numbers big. font-variant-numeric: tabular-nums stops the digits jiggling as they change, because every digit takes the same width.
body {
margin: 0;
min-height: 100vh;
display: grid;
place-items: center;
font-family: system-ui, sans-serif;
background: #0f172a;
color: #f8fafc;
}
.timer { text-align: center; }
#time {
font-size: clamp(4rem, 18vw, 9rem);
font-variant-numeric: tabular-nums;
margin: 0.2em 0;
}
button {
font-size: 1rem;
padding: 0.7em 1.6em;
margin: 0 0.3em;
border: 0;
border-radius: 999px;
cursor: pointer;
}
#start { background: #f59e0b; color: #0f172a; }
Step 3: the state
Everything the timer needs to know fits in a handful of variables. Keeping the durations in one object makes them easy to change later.
const MODES = {
focus: { label: 'Focus', minutes: 25 },
short: { label: 'Short break', minutes: 5 },
long: { label: 'Long break', minutes: 15 },
};
let mode = 'focus';
let remaining = MODES.focus.minutes * 60 * 1000; // milliseconds
let endTime = null;
let tick = null; // the interval ID while running
let sessions = 0;
Step 4: why we don't just count down
The obvious approach is to subtract one every second with setInterval. The problem is that setInterval isn't precise. Each tick can land a little late, and browsers deliberately slow timers down in background tabs to save battery. A timer that subtracts one per tick slowly falls behind the real clock, and in a background tab it can fall well behind.
The fix: when the timer starts, work out the exact time it should end. On every tick, the time left is simply the end time minus now. If a tick is late, the next one corrects itself.
function start() {
endTime = Date.now() + remaining;
tick = setInterval(() => {
remaining = Math.max(0, endTime - Date.now());
render();
if (remaining === 0) finish();
}, 250);
startBtn.textContent = 'Pause';
}
Ticking every 250ms instead of every 1000ms means the display never looks like it skipped a second.
Step 5: rendering
render() turns milliseconds into mm:ss and updates the page and the tab title. padStart adds the leading zero, so 5 seconds shows as 05.
function render() {
const total = Math.ceil(remaining / 1000);
const mm = String(Math.floor(total / 60)).padStart(2, '0');
const ss = String(total % 60).padStart(2, '0');
timeEl.textContent = `${mm}:${ss}`;
modeEl.textContent = MODES[mode].label;
countEl.textContent = `Sessions done: ${sessions}`;
document.title = `${mm}:${ss} · ${MODES[mode].label}`;
}
Step 6: switching between focus and breaks
When a block finishes, we stop the timer, play a beep, and move to the next mode. Every fourth focus session earns the long break.
function finish() {
pause();
beep();
if (mode === 'focus') {
sessions++;
setMode(sessions % 4 === 0 ? 'long' : 'short');
} else {
setMode('focus');
}
}
The beep uses the Web Audio API, so there's no sound file to download. Browsers only allow audio after the user has interacted with the page, but pressing Start counts, so it just works.
function beep() {
const ctx = new AudioContext();
const osc = ctx.createOscillator();
osc.connect(ctx.destination);
osc.start();
osc.stop(ctx.currentTime + 0.4);
}
The full app.js
Here's everything together:
const MODES = {
focus: { label: 'Focus', minutes: 25 },
short: { label: 'Short break', minutes: 5 },
long: { label: 'Long break', minutes: 15 },
};
const timeEl = document.getElementById('time');
const modeEl = document.getElementById('mode');
const countEl = document.getElementById('count');
const startBtn = document.getElementById('start');
const resetBtn = document.getElementById('reset');
let mode = 'focus';
let remaining = MODES.focus.minutes * 60 * 1000;
let endTime = null;
let tick = null;
let sessions = 0;
function render() {
const total = Math.ceil(remaining / 1000);
const mm = String(Math.floor(total / 60)).padStart(2, '0');
const ss = String(total % 60).padStart(2, '0');
timeEl.textContent = `${mm}:${ss}`;
modeEl.textContent = MODES[mode].label;
countEl.textContent = `Sessions done: ${sessions}`;
document.title = `${mm}:${ss} · ${MODES[mode].label}`;
}
function setMode(next) {
mode = next;
remaining = MODES[mode].minutes * 60 * 1000;
render();
}
function beep() {
const ctx = new AudioContext();
const osc = ctx.createOscillator();
osc.connect(ctx.destination);
osc.start();
osc.stop(ctx.currentTime + 0.4);
}
function pause() {
clearInterval(tick);
tick = null;
startBtn.textContent = 'Start';
}
function finish() {
pause();
beep();
if (mode === 'focus') {
sessions++;
setMode(sessions % 4 === 0 ? 'long' : 'short');
} else {
setMode('focus');
}
}
function start() {
endTime = Date.now() + remaining;
tick = setInterval(() => {
remaining = Math.max(0, endTime - Date.now());
render();
if (remaining === 0) finish();
}, 250);
startBtn.textContent = 'Pause';
}
const toggle = () => (tick ? pause() : start());
startBtn.addEventListener('click', toggle);
resetBtn.addEventListener('click', () => { pause(); setMode(mode); });
document.addEventListener('keydown', (e) => {
if (e.code === 'Space') { e.preventDefault(); toggle(); }
});
render();
Open index.html in a browser and it runs. To test the flow without waiting 25 minutes, temporarily set the minutes in MODES to something like 0.1.
Ideas to take it further
-
Custom durations. Add inputs for focus and break length and write them into
MODES. -
Remember progress. Save
sessionstolocalStorageso the count survives a refresh. - Notifications. Use the Notifications API so you get a pop-up when a block ends, even if the tab is hidden.
-
A progress ring. Draw a circle with SVG or a
conic-gradientthat empties as time runs down. - Daily stats. Store each finished session with a date and show how many you did this week.
It's a small project, but it covers the patterns you'll reuse constantly: keeping state in one place, deriving the display from that state, and not trusting setInterval to keep time.
Written by the team at NextStepAcademy, an Australian tutoring company. We're big fans of short, focused study blocks for exam prep.
Top comments (0)