DEV Community

Arhan Khan
Arhan Khan

Posted on

Build a Meditation Timer App with JavaScript

A meditation timer is a perfect first project: it is small, useful, and it teaches you timers, state, and DOM updates in one go. Today we will build one with plain HTML, CSS, and JavaScript — no framework needed.

Start with the structure. We need a display, a few duration buttons, and a start button:

<div class="timer-app">
  <h1 id="display">10:00</h1>
  <div class="durations">
    <button data-minutes="5">5 min</button>
    <button data-minutes="10">10 min</button>
    <button data-minutes="20">20 min</button>
  </div>
  <button id="startBtn">Start</button>
  <button id="resetBtn">Reset</button>
</div>
Enter fullscreen mode Exit fullscreen mode

The JavaScript keeps one piece of state — seconds remaining — and updates the screen every second:

let totalSeconds = 600;
let remaining = totalSeconds;
let intervalId = null;

const display = document.getElementById("display");

function format(seconds) {
  const m = Math.floor(seconds / 60);
  const s = seconds % 60;
  return `${String(m).padStart(2, "0")}:${String(s).padStart(2, "0")}`;
}

function tick() {
  remaining--;
  display.textContent = format(remaining);
  if (remaining <= 0) {
    clearInterval(intervalId);
    intervalId = null;
    display.textContent = "Done. Breathe easy.";
    playBell();
  }
}

document.getElementById("startBtn").addEventListener("click", () => {
  if (intervalId) return; // already running
  intervalId = setInterval(tick, 1000);
});

document.getElementById("resetBtn").addEventListener("click", () => {
  clearInterval(intervalId);
  intervalId = null;
  remaining = totalSeconds;
  display.textContent = format(remaining);
});
Enter fullscreen mode Exit fullscreen mode

A few details make it feel like a real app. Add a soft bell sound with the Web Audio API when the timer ends, so the user does not have to watch the screen. Add a progress ring with SVG that shrinks as time passes — one <circle> and a stroke-dashoffset update in the tick function is all it takes. And save the chosen duration in localStorage so the app remembers the user's preference next time.

The best part of a project like this is that it teaches a real pattern: one source of truth (remaining), one function that renders it (format + text update), and events that only change state. That pattern shows up in every framework you will ever learn.

A meditation timer is also a good reminder that small daily habits compound. Atomic Habits makes the same point about tiny routines: ten quiet minutes a day, repeated, changes more than one heroic hour a month ever will. The app you just built is the tool that makes the small version easy to repeat.

Extensions to try next: a breathing animation that grows and shrinks with inhale and exhale, a session counter that stores completed meditations in localStorage, and a gentle chime every five minutes for longer sessions. Each one is a small, satisfying feature on its own.

Top comments (0)