Interaction · Complete runnable example

Pausable duration countdown

A timer the reader starts and controls, without urgency or automatic actions.

Live example

This preview runs in an isolated document. Clipboard writes and saved preferences may be blocked, and printing requires the standalone file. A dialog affects only the example. Reset reloads the original example state.

When to use it

Use this example for a user-chosen break or short activity. It accepts whole seconds from 1 to 3600, starts only on request and keeps completion feedback visible. The timer display is not a live announcement every second.

Try the important details

  1. Set a short duration and check that completion is announced once.
  2. Pause, wait, then resume. Paused time does not consume the remaining duration.
  3. Switch away while running, then return. The display catches up to elapsed time instead of counting delayed callbacks.

How it works

  • The deadline is computed when starting or resuming. Scheduled callbacks refresh the display rather than decrementing a counter.
  • Hidden documents stop scheduled repaint work. A visibility change recomputes the remaining duration from the deadline.
  • State transitions update the controls together and guard completion against repeated announcements.

The complete source

The preview, copied text, and downloaded file use this same document.

HTML

<!doctype html>
<!--
MIT License
Copyright (c) 2026 MRHTML

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-->
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta
      http-equiv="Content-Security-Policy"
      content="default-src 'none'; style-src 'unsafe-inline'; script-src 'unsafe-inline'; img-src data: blob:; connect-src 'none'; form-action 'none'; object-src 'none'; base-uri 'none'"
    />
    <title>Make a little space. | MRHTML example</title>
    <style>
      :root {
        color-scheme: light;
        --paper: #f5f3ee;
        --surface: #fff;
        --ink: #172a3b;
        --muted: #536575;
        --line: #d2dce1;
        --blue: #234ed8;
        --soft: #eef3ff;
        --orange: #ad4b17;
        --error: #a62440;
        font:
          16px/1.6 system-ui,
          -apple-system,
          BlinkMacSystemFont,
          'Segoe UI',
          sans-serif;
        color: var(--ink);
        background: var(--paper);
      }
      * {
        box-sizing: border-box;
      }
      body {
        margin: 0;
        padding: clamp(24px, 6vw, 64px) 22px;
        min-height: 100svh;
      }
      main {
        width: min(760px, 100%);
        margin: auto;
        min-width: 0;
      }
      h1,
      h2,
      h3,
      p {
        margin: 0;
      }
      h1 {
        font-size: clamp(2rem, 6vw, 3rem);
        line-height: 1.08;
        letter-spacing: -0.055em;
        font-weight: 680;
        max-width: 680px;
      }
      h2 {
        font-size: 1.2rem;
        line-height: 1.3;
        letter-spacing: -0.02em;
      }
      p {
        color: var(--muted);
      }
      header {
        margin-bottom: 28px;
      }
      .brand {
        font:
          700 0.73rem/1.5 ui-monospace,
          monospace;
        letter-spacing: 0.1em;
        text-transform: uppercase;
        color: var(--blue);
        margin-bottom: 20px;
      }
      .intro {
        max-width: 62ch;
        margin-top: 16px;
      }
      .surface {
        background: var(--surface);
        border: 1px solid var(--line);
        border-radius: 18px;
        padding: clamp(20px, 4vw, 32px);
      }
      .note {
        font-size: 0.85rem;
        margin-top: 20px;
      }
      .eyebrow {
        font:
          600 0.72rem/1.5 ui-monospace,
          monospace;
        letter-spacing: 0.08em;
        text-transform: uppercase;
      }
      .actions {
        display: flex;
        flex-wrap: wrap;
        gap: 10px;
        margin-top: 20px;
      }
      button,
      input,
      textarea,
      select {
        font: inherit;
      }
      button {
        border: 1px solid var(--ink);
        background: var(--ink);
        color: #fff;
        border-radius: 8px;
        padding: 10px 17px;
        min-height: 44px;
        font-weight: 650;
        cursor: pointer;
      }
      button.secondary {
        background: transparent;
        color: var(--ink);
        border-color: var(--line);
      }
      button:hover:not(:disabled) {
        background: var(--blue);
        border-color: var(--blue);
        color: #fff;
      }
      button:disabled {
        cursor: not-allowed;
        opacity: 0.55;
      }
      label {
        font-weight: 600;
      }
      input:not([type='checkbox']):not([type='range']):not([type='file']),
      textarea,
      select {
        width: 100%;
        min-width: 0;
        border: 1px solid #8e9faa;
        border-radius: 8px;
        padding: 10px 12px;
        background: #fff;
        color: var(--ink);
      }
      textarea {
        resize: vertical;
        min-height: 180px;
      }
      input[type='range'] {
        width: 100%;
        accent-color: var(--blue);
      }
      input[type='checkbox'] {
        width: 20px;
        height: 20px;
        accent-color: var(--blue);
      }
      :focus-visible {
        outline: 3px solid var(--orange);
        outline-offset: 4px;
      }
      a {
        color: var(--blue);
      }
      .status {
        min-height: 1.6em;
        font-size: 0.9rem;
        margin-top: 18px;
        overflow-wrap: anywhere;
      }
      .status[data-error='true'] {
        color: var(--error);
      }
      [hidden] {
        display: none !important;
      }
      .sr-only {
        position: absolute;
        width: 1px;
        height: 1px;
        padding: 0;
        margin: -1px;
        overflow: hidden;
        clip: rect(0, 0, 0, 0);
        white-space: nowrap;
        border: 0;
      }
      .field {
        display: grid;
        gap: 7px;
        margin-top: 16px;
      }
      footer {
        font-size: 0.75rem;
        color: var(--muted);
        border-top: 1px solid var(--line);
        margin-top: 30px;
        padding-top: 16px;
        display: flex;
        justify-content: space-between;
        gap: 12px;
      }
      noscript {
        display: block;
        padding: 14px;
        background: #fff2d9;
        border: 1px solid #ad4b17;
        margin-top: 20px;
        border-radius: 8px;
        color: var(--ink);
      }
      @media (max-width: 400px) {
        body {
          padding: 25px 16px;
        }
        .surface {
          padding: 20px;
        }
        button {
          padding: 10px 14px;
        }
      }
      @media (prefers-reduced-motion: reduce) {
        *,
        *::before,
        *::after {
          scroll-behavior: auto !important;
          animation: none !important;
          transition: none !important;
        }
      }

      .timer-surface {
        background: var(--ink);
        color: #fff;
        border-radius: 20px;
        padding: clamp(25px, 5vw, 45px);
        text-align: center;
      }
      .timer-surface p {
        color: #d4e0e8;
      }
      #time-left {
        font:
          500 clamp(4rem, 16vw, 7.2rem)/1.2 ui-monospace,
          monospace;
        letter-spacing: -0.08em;
        margin: 25px 0 5px;
        font-variant-numeric: tabular-nums;
      }
      .duration-row {
        display: flex;
        justify-content: center;
        align-items: center;
        flex-wrap: wrap;
        gap: 14px;
        margin-top: 30px;
        font-size: 0.9rem;
      }
      #duration {
        width: 110px;
        text-align: center;
      }
      #duration-help {
        font-size: 0.75rem;
        margin-top: 8px;
      }
      .timer-surface .actions {
        justify-content: center;
        margin-top: 28px;
      }
      .timer-surface button {
        background: #d7ed9c;
        color: #1d2f23;
        border-color: #d7ed9c;
      }
      .timer-surface button.secondary {
        background: transparent;
        color: #fff;
        border-color: #8e9faa;
      }
      .timer-surface .status[data-error='true'] {
        color: #ffc1cb;
      }
    </style>
  </head>
  <body>
    <main>
      <header>
        <p class="brand">MRHTML / Runnable example</p>
        <h1>Make a little space.</h1>
        <p class="intro">
          A timer for a short reading, stretching or thinking break. You choose
          when it starts and stops.
        </p>
      </header>
      <section class="timer-surface">
        <p class="eyebrow">Your time, at your pace</p>
        <div
          id="time-left"
          role="timer"
          aria-live="off"
          aria-label="Time remaining"
        >
          01:30
        </div>
        <p id="timer-state">Ready when you are.</p>
        <div class="duration-row">
          <label for="duration">Duration in seconds</label
          ><input
            id="duration"
            type="number"
            min="1"
            max="3600"
            step="1"
            value="90"
            disabled
            aria-describedby="duration-help"
          />
        </div>
        <p id="duration-help">Choose a whole number from 1 to 3600.</p>
        <div class="actions">
          <button id="start-timer" type="button" disabled>Start timer</button
          ><button id="pause-timer" class="secondary" type="button" disabled>
            Pause</button
          ><button id="reset-timer" class="secondary" type="button" disabled>
            Reset
          </button>
        </div>
        <p id="timer-status" class="status" role="status" aria-atomic="true">
          The timer has not started.
        </p>
      </section>
      <p class="note">
        Time continues while this tab is in the background. Pause preserves the
        remaining duration. There is no sound, deadline offer or automatic
        action when time finishes. This duration demo uses the device clock;
        changing that clock can change the remaining time.
      </p>
      <noscript
        >The content is readable without JavaScript. Interactive controls are
        unavailable.</noscript
      >
      <footer>
        <span>MRHTML example</span><span>Original code · MIT</span>
      </footer>
    </main>
    <script>
      'use strict';
      (() => {
        const input = document.querySelector('#duration'),
          start = document.querySelector('#start-timer'),
          pause = document.querySelector('#pause-timer'),
          reset = document.querySelector('#reset-timer'),
          display = document.querySelector('#time-left'),
          state = document.querySelector('#timer-state'),
          status = document.querySelector('#timer-status');
        let remaining = 90000,
          deadline = 0,
          running = false,
          completed = false,
          timer = 0;
        const announce = (text, error = false) => {
          status.textContent = text;
          status.dataset.error = String(error);
        };
        function validDuration() {
          const value = Number(input.value);
          if (!Number.isInteger(value) || value < 1 || value > 3600) {
            announce('Enter a whole number of seconds from 1 to 3600.', true);
            input.focus();
            return null;
          }
          return value * 1000;
        }
        function render() {
          const seconds = Math.ceil(Math.max(0, remaining) / 1000);
          display.textContent = `${String(Math.floor(seconds / 60)).padStart(2, '0')}:${String(seconds % 60).padStart(2, '0')}`;
          pause.disabled = !running;
          start.disabled = running;
          input.disabled = running;
          start.textContent = completed
            ? 'Start again'
            : remaining < Number(input.value) * 1000
              ? 'Resume'
              : 'Start timer';
        }
        function finish() {
          if (completed) return;
          completed = true;
          running = false;
          remaining = 0;
          clearTimeout(timer);
          state.textContent = 'Your time is complete.';
          announce('Timer complete. Take the next step when you are ready.');
          render();
        }
        function tick() {
          clearTimeout(timer);
          if (!running) return;
          remaining = Math.max(0, deadline - Date.now());
          if (remaining === 0) {
            finish();
            return;
          }
          render();
          if (!document.hidden) timer = setTimeout(tick, 200);
        }
        start.addEventListener('click', () => {
          if (running || validDuration() === null) return;
          if (completed) {
            const duration = validDuration();
            if (duration === null) return;
            remaining = duration;
            completed = false;
          }
          if (remaining <= 0) return;
          running = true;
          deadline = Date.now() + remaining;
          state.textContent = 'A little time for yourself.';
          announce('Timer running.');
          tick();
        });
        pause.addEventListener('click', () => {
          if (!running) return;
          remaining = Math.max(0, deadline - Date.now());
          if (!remaining) {
            finish();
            return;
          }
          running = false;
          clearTimeout(timer);
          state.textContent = 'Paused. Come back when you are ready.';
          announce('Timer paused.');
          render();
        });
        function setDuration() {
          if (running) return;
          const value = validDuration();
          if (value === null) return;
          remaining = value;
          completed = false;
          state.textContent = 'Ready when you are.';
          announce('Duration updated. The timer has not started.');
          render();
        }
        input.addEventListener('change', setDuration);
        reset.addEventListener('click', () => {
          clearTimeout(timer);
          running = false;
          input.disabled = false;
          const value = validDuration();
          remaining = value === null ? 90000 : value;
          if (value === null) input.value = '90';
          completed = false;
          state.textContent = 'Ready when you are.';
          announce('Timer reset.');
          render();
        });
        document.addEventListener('visibilitychange', () => {
          if (running) tick();
        });
        input.disabled = false;
        start.disabled = false;
        reset.disabled = false;
        render();
      })();
    </script>
  </body>
</html>