The user wants me to create a web-based stopwatch with the following features:
- Start button - starts the timer
- Stop button - stops the timer
- Pause button - pauses the timer (can be resumed)
- Reset button - resets the timer to 00:00:00
Wait, typically a stopwatch has:
- Start/Resume
- Pause (to temporarily stop)
- Stop (to completely stop)
- Reset (to go back to zero)
Actually, in most stopwatches, "Stop" and "Pause" are often the same thing. Let me think about this differently:
Common stopwatch functionality:
- Start - begins counting from current time
- Pause - temporarily stops counting (can resume)
- Stop - stops counting completely (usually same as pause)
- Reset - sets time back to zero
I'll create a stopwatch that has:
- Start button (starts or resumes the timer)
- Pause button (pauses the timer)
- Stop button (stops the timer, similar to pause but semantically different)
- Reset button (resets to 00:00:00:00)
Actually, let me make it simpler and more intuitive:
- Start - starts the timer
- Pause - pauses the timer (and changes to Resume when paused)
- Stop - stops the timer
- Reset - resets to zero
Or I could do:
- Start/Pause toggle
- Stop
- Reset
Let me create a comprehensive stopwatch with all four buttons as requested:
- Start - starts the timer if stopped or paused
- Pause - pauses the timer
- Stop - stops the timer (resets the running state but keeps the time)
- Reset - resets everything to zero
I'll include:
- Hours:Minutes:Seconds:Milliseconds display
- Nice styling with CSS
- Lap functionality could be a nice addition
Let me create a clean, modern-looking stopwatch with HTML, CSS, and JavaScript.
I'll develop a structured approach to build the stopwatch, focusing on clear functionality and user experience. The implementation will involve creating a precise timing mechanism with distinct button behaviors. I'll ensure the interface is intuitive, with clear state management for start, pause, stop, and reset actions. The goal is to create a responsive and user-friendly stopwatch that provides accurate time tracking. # Web-Based Stopwatch
Here's a complete stopwatch with start, stop, pause, and reset functionality, plus a lap times feature:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Stopwatch</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
padding: 20px;
}
.stopwatch-container {
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
border-radius: 30px;
padding: 40px;
box-shadow: 0 25px 45px rgba(0, 0, 0, 0.3),
inset 0 0 0 1px rgba(255, 255, 255, 0.1);
text-align: center;
min-width: 400px;
}
h1 {
color: #fff;
margin-bottom: 30px;
font-size: 2rem;
text-transform: uppercase;
letter-spacing: 3px;
}
.display {
background: rgba(0, 0, 0, 0.3);
border-radius: 15px;
padding: 30px;
margin-bottom: 30px;
border: 2px solid rgba(255, 255, 255, 0.1);
}
.time {
font-family: 'Courier New', monospace;
font-size: 4rem;
color: #00ff88;
text-shadow: 0 0 20px rgba(0, 255, 136, 0.5);
letter-spacing: 2px;
}
.milliseconds {
font-size: 2.5rem;
color: #00cc6a;
}
.buttons {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 15px;
margin-bottom: 25px;
}
button {
padding: 15px 30px;
font-size: 1.1rem;
font-weight: bold;
text-transform: uppercase;
letter-spacing: 2px;
border: none;
border-radius: 10px;
cursor: pointer;
transition: all 0.3s ease;
position: relative;
overflow: hidden;
}
button::before {
content: '';
position: absolute;
top: 0;
left: -100%;
width: 100%;
height: 100%;
background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
transition: left 0.5s ease;
}
button:hover::before {
left: 100%;
}
button:active {
transform: scale(0.95);
}
.start-btn {
background: linear-gradient(135deg, #00ff88, #00cc6a);
color: #1a1a2e;
box-shadow: 0 5px 20px rgba(0, 255, 136, 0.4);
}
.start-btn:hover {
box-shadow: 0 8px 30px rgba(0, 255, 136, 0.6);
transform: translateY(-2px);
}
.pause-btn {
background: linear-gradient(135deg, #ffc107, #ff9800);
color: #1a1a2e;
box-shadow: 0 5px 20px rgba(255, 193, 7, 0.4);
}
.pause-btn:hover {
box-shadow: 0 8px 30px rgba(255, 193, 7, 0.6);
transform: translateY(-2px);
}
.stop-btn {
background: linear-gradient(135deg, #ff4757, #ff3838);
color: #fff;
box-shadow: 0 5px 20px rgba(255, 71, 87, 0.4);
}
.stop-btn:hover {
box-shadow: 0 8px 30px rgba(255, 71, 87, 0.6);
transform: translateY(-2px);
}
.reset-btn {
background: linear-gradient(135deg, #5f27cd, #341f97);
color: #fff;
box-shadow: 0 5px 20px rgba(95, 39, 205, 0.4);
}
.reset-btn:hover {
box-shadow: 0 8px 30px rgba(95, 39, 205, 0.6);
transform: translateY(-2px);
}
.lap-btn {
grid-column: span 2;
background: linear-gradient(135deg, #00d2d3, #0093a8);
color: #fff;
box-shadow: 0 5px 20px rgba(0, 210, 211, 0.4);
}
.lap-btn:hover {
box-shadow: 0 8px 30px rgba(0, 210, 211, 0.6);
transform: translateY(-2px);
}
button:disabled {
opacity: 0.5;
cursor: not-allowed;
transform: none !important;
box-shadow: none !important;
}
.laps-container {
max-height: 200px;
overflow-y: auto;
margin-top: 20px;
}
.laps-container::-webkit-scrollbar {
width: 8px;
}
.laps-container::-webkit-scrollbar-track {
background: rgba(255, 255, 255, 0.1);
border-radius: 10px;
}
.laps-container::-webkit-scrollbar-thumb {
background: rgba(0, 255, 136, 0.5);
border-radius: 10px;
}
.lap-title {
color: #fff;
font-size: 1.2rem;
margin-bottom: 15px;
display: none;
}
.lap-title.show {
display: block;
}
.lap-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 12px 20px;
background: rgba(255, 255, 255, 0.05);
border-radius: 8px;
margin-bottom: 8px;
color: #fff;
font-family: 'Courier New', monospace;
animation: slideIn 0.3s ease;
}
.lap-item:hover {
background: rgba(255, 255, 255, 0.1);
}
.lap-number {
color: #00ff88;
font-weight: bold;
}
.lap-time {
color: #ffc107;
}
.lap-diff {
color: #aaa;
font-size: 0.9rem;
}
@keyframes slideIn {
from {
opacity: 0;
transform: translateX(-20px);
}
to {
opacity: 1;
transform: translateX(0);
}
}
.status {
color: #888;
font-size: 0.9rem;
margin-top: 15px;
text-transform: uppercase;
letter-spacing: 2px;
}
.status.running {
color: #00ff88;
}
.status.paused {
color: #ffc107;
}
.status.stopped {
color: #ff4757;
}
/* Pulse animation for running state */
.display.running {
animation: pulse 1s ease-in-out infinite;
}
@keyframes pulse {
0%, 100% {
border-color: rgba(0, 255, 136, 0.3);
}
50% {
border-color: rgba(0, 255, 136, 0.8);
}
}
@media (max-width: 480px) {
.stopwatch-container {
min-width: auto;
width: 100%;
padding: 25px;
}
.time {
font-size: 2.5rem;
}
.milliseconds {
font-size: 1.5rem;
}
button {
padding: 12px 20px;
font-size: 0.9rem;
}
h1 {
font-size: 1.5rem;
}
}
</style>
</head>
<body>
<div class="stopwatch-container">
<h1>⏱️ Stopwatch</h1>
<div class="display" id="display">
<div class="time">
<span id="hours">00</span>:<span id="minutes">00</span>:<span id="seconds">00</span><span class="milliseconds">.<span id="milliseconds">000</span></span>
</div>
</div>
<div class="buttons">
<button class="start-btn" id="startBtn" onclick="start()">▶ Start</button>
<button class="pause-btn" id="pauseBtn" onclick="pause()" disabled>⏸ Pause</button>
<button class="stop-btn" id="stopBtn" onclick="stop()" disabled>⏹ Stop</button>
<button class="reset-btn" id="resetBtn" onclick="reset()">↺ Reset</button>
<button class="lap-btn" id="lapBtn" onclick="recordLap()" disabled>🏁 Lap</button>
</div>
<div class="status" id="status">Ready</div>
<h3 class="lap-title" id="lapTitle">Lap Times</h3>
<div class="laps-container" id="lapsContainer"></div>
</div>
<script>
// Stopwatch state
let startTime = 0;
let elapsedTime = 0;
let timerInterval = null;
let isRunning = false;
let isPaused = false;
let laps = [];
let lastLapTime = 0;
// DOM Elements
const hoursEl = document.getElementById('hours');
const minutesEl = document.getElementById('minutes');
const secondsEl = document.getElementById('seconds');
const millisecondsEl = document.getElementById('milliseconds');
const displayEl = document.getElementById('display');
const statusEl = document.getElementById('status');
const startBtn = document.getElementById('startBtn');
const pauseBtn = document.getElementById('pauseBtn');
const stopBtn = document.getElementById('stopBtn');
const resetBtn = document.getElementById('resetBtn');
const lapBtn = document.getElementById('lapBtn');
const lapTitle = document.getElementById('lapTitle');
const lapsContainer = document.getElementById('lapsContainer');
// Format time with leading zeros
function formatTime(time) {
const hours = Math.floor(time / 3600000);
const minutes = Math.floor((time % 3600000) / 60000);
const seconds = Math.floor((time % 60000) / 1000);
const milliseconds = time % 1000;
return {
hours: hours.toString().padStart(2, '0'),
minutes: minutes.toString().padStart(2, '0'),
seconds: seconds.toString().padStart(2, '0'),
milliseconds: milliseconds.toString().padStart(3, '0')
};
}
// Update display
function updateDisplay() {
const time = formatTime(elapsedTime);
hoursEl.textContent = time.hours;
minutesEl.textContent = time.minutes;
secondsEl.textContent = time.seconds;
millisecondsEl.textContent = time.milliseconds;
}
// Update elapsed time
function updateTime() {
elapsedTime = Date.now() - startTime;
updateDisplay();
}
// Start stopwatch
function start() {
if (!isRunning) {
isRunning = true;
isPaused = false;
startTime = Date.now() - elapsedTime;
timerInterval = setInterval(updateTime, 10);
// Update UI
displayEl.classList.add('running');
statusEl.textContent = 'Running';
statusEl.className = 'status running';
startBtn.disabled = true;
pauseBtn.disabled = false;
stopBtn.disabled = false;
lapBtn.disabled = false;
}
}
// Pause stopwatch
function pause() {
if (isRunning && !isPaused) {
isPaused = true;
isRunning = false;
clearInterval(timerInterval);
// Update UI
displayEl.classList.remove('running');
statusEl.textContent = 'Paused';
statusEl.className = 'status paused';
startBtn.disabled = false;
startBtn.innerHTML = '▶ Resume';
pauseBtn.disabled = true;
}
}
// Stop stopwatch
function stop() {
if (isRunning || isPaused) {
isRunning = false;
isPaused = false;
clearInterval(timerInterval);
// Update UI
displayEl.classList.remove('running');
statusEl.textContent = 'Stopped';
statusEl.className = 'status stopped';
startBtn.disabled = false;
startBtn.innerHTML = '▶ Start';
pauseBtn.disabled = true;
stopBtn.disabled = true;
lapBtn.disabled = true;
}
}
// Reset stopwatch
function reset() {
isRunning = false;
isPaused = false;
clearInterval(timerInterval);
elapsedTime = 0;
lastLapTime = 0;
laps = [];
// Update display
updateDisplay();
// Update UI
displayEl.classList.remove('running');
statusEl.textContent = 'Ready';
statusEl.className = 'status';
startBtn.disabled = false;
startBtn.innerHTML = '▶ Start';
pauseBtn.disabled = true;
stopBtn.disabled = true;
lapBtn.disabled = true;
// Clear laps
lapTitle.classList.remove('show');
lapsContainer.innerHTML = '';
}
// Record lap time
function recordLap() {
if (isRunning) {
const lapTime = elapsedTime;
const lapDiff = lapTime - lastLapTime;
lastLapTime = lapTime;
laps.unshift({
number: laps.length + 1,
time: lapTime,
diff: lapDiff
});
// Update laps display
lapTitle.classList.add('show');
renderLaps();
}
}
// Render lap times
function renderLaps() {
lapsContainer.innerHTML = '';
laps.forEach((lap, index) => {
const lapEl = document.createElement('div');
lapEl.className = 'lap-item';
const time = formatTime(lap.time);
const diff = formatTime(lap.diff);
lapEl.innerHTML = `
<span class="lap-number">Lap ${lap.number}</span>
<span class="lap-diff">+${diff.minutes}:${diff.seconds}.${diff.milliseconds}</span>
<span class="lap-time">${time.hours}:${time.minutes}:${time.seconds}.${time.milliseconds}</span>
`;
lapsContainer.appendChild(lapEl);
});
}
// Keyboard shortcuts
document.addEventListener('keydown', (e) => {
switch(e.code) {
case 'Space':
e.preventDefault();
if (isRunning) {
pause();
} else {
start();
}
break;
case 'KeyR':
reset();
break;
case 'KeyL':
if (isRunning) recordLap();
break;
case 'KeyS':
stop();
break;
}
});
// Initialize display
updateDisplay();
</script>
</body>
</html>
Features
⏱️ Core Functionality
| Button | Action |
|---|
| Start | Begins the stopwatch / Resumes after pause |
| Pause | Temporarily pauses the stopwatch |
| Stop | Completely stops the stopwatch |
| Reset | Clears time back to zero |
| Lap | Records lap times while running |
⌨️ Keyboard Shortcuts
- Space - Start/Pause toggle
- S - Stop
- R - Reset
- L - Record lap
🎨 Visual Features
- Modern glassmorphism design
- Animated pulsing border when running
- Color-coded status indicator
- Smooth button hover effects
- Responsive design for mobile devices
- Scrollable lap times list
📊 Time Display
- Hours, minutes, seconds, and milliseconds
- Lap times with difference from previous lap
- High-precision timing (10ms intervals)