author Angelo Gladding
name Timer
published 2026-10-09T01:51:11.402406-07:00
type entry
updated 2026-10-13T15:31:37.262549-07:00
url /timer, /2026/10/09/ru
visibility public
Content
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | <form class=timer-settings> <label>Minutes <input name=minutes type=number min=0 max=999 step=1 value=5 required></label> <label>Seconds <input name=seconds type=number min=0 max=59 step=1 value=0 required></label> </form> <output class=timer-display aria-label="Time remaining"></output> <progress class=timer-progress max=1 value=0 aria-label="Elapsed time"></progress> <div class=timer-controls> <button type=button class=timer-toggle>Start</button> <button type=button class=timer-reset>Reset</button> </div> <label class=timer-sound><input type=checkbox checked> Sound when finished</label> <p class=timer-status role=status>Ready</p> <style> .timer-settings, .timer-controls { display: flex; justify-content: center; gap: .6rem; flex-wrap: wrap; } .timer-settings label { display: grid; gap: .35rem; text-align: left; } .timer-display { display: block; margin: 1rem 0 .5rem; font-size: clamp(3rem, 15vw, 5rem); font-variant-numeric: tabular-nums; } .timer-progress { width: 100%; margin-bottom: 1rem; } .timer-sound { display: block; margin-top: 1rem; } .timer-status { min-height: 1.5em; margin-bottom: 0; } .timer.finished .timer-display { color: #c05020; } </style> <script> (() => { const root = document.querySelector("article#content"); const form = root.querySelector("form"); const minutes = form.elements.minutes; const seconds = form.elements.seconds; const display = root.querySelector("output"); const progress = root.querySelector("progress"); const toggle = root.querySelector(".timer-toggle"); const resetButton = root.querySelector(".timer-reset"); const status = root.querySelector(".timer-status"); const sound = root.querySelector('[type="checkbox"]'); const originalTitle = document.title; let duration = 0; let remaining = 0; let deadline = 0; let running = false; let interval = null; let audio = null; function readDuration() { return (Number(minutes.value) * 60 + Number(seconds.value)) * 1000; } function format(ms) { const total = Math.ceil(ms / 1000); return String(Math.floor(total / 60)).padStart(2, "0") + ":" + String(total % 60).padStart(2, "0"); } function paint() { const text = format(remaining); display.textContent = text; progress.value = duration ? 1 - remaining / duration : 0; toggle.textContent = running ? "Pause" : remaining < duration && remaining > 0 ? "Resume" : "Start"; minutes.disabled = seconds.disabled = running || remaining < duration; root.querySelectorAll("[data-minutes]").forEach(button => { button.disabled = minutes.disabled; }); document.title = running ? text + " · Timer" : originalTitle; } function prepareAudio() { if (!sound.checked) return; try { const AudioContext = window.AudioContext || window.webkitAudioContext; audio = audio || new AudioContext(); audio.resume().catch(() => {}); } catch (_) {} } function beep() { if (!sound.checked || !audio || audio.state !== "running") return; const now = audio.currentTime; for (let i = 0; i < 3; i++) { const oscillator = audio.createOscillator(); const gain = audio.createGain(); const start = now + i * .3; oscillator.frequency.value = 880; gain.gain.setValueAtTime(0, start); gain.gain.linearRampToValueAtTime(.15, start + .01); gain.gain.exponentialRampToValueAtTime(.001, start + .2); oscillator.connect(gain); gain.connect(audio.destination); oscillator.start(start); oscillator.stop(start + .21); } } function stop() { running = false; clearInterval(interval); interval = null; } function tick() { remaining = Math.max(0, deadline - Date.now()); if (remaining === 0) { stop(); paint(); root.classList.add("finished"); status.textContent = "Time’s up!"; document.title = "Time’s up! · " + originalTitle; beep(); return; } paint(); } function reset() { stop(); duration = remaining = readDuration(); root.classList.remove("finished"); status.textContent = "Ready."; paint(); } function startOrPause() { if (running) { tick(); if (!running) return; stop(); status.textContent = "Paused."; paint(); return; } if (!form.reportValidity()) return; if (remaining === duration || remaining === 0) { duration = remaining = readDuration(); } if (remaining <= 0) { status.textContent = "Enter a duration greater than zero."; minutes.focus(); return; } prepareAudio(); root.classList.remove("finished"); deadline = Date.now() + remaining; running = true; status.textContent = "Running."; tick(); interval = setInterval(tick, 200); } toggle.addEventListener("click", startOrPause); resetButton.addEventListener("click", reset); form.addEventListener("submit", event => { event.preventDefault(); startOrPause(); }); form.addEventListener("input", reset); root.querySelectorAll("[data-minutes]").forEach(button => { button.addEventListener("click", () => { minutes.value = button.dataset.minutes; seconds.value = 0; reset(); }); }); document.addEventListener("visibilitychange", () => { if (running) tick(); }); reset(); })(); </script> |