<!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;
background: linear-gradient(135deg, #1e3c72, #2a5298);
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.container {
text-align: center;
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
border-radius: 20px;
padding: 3rem 2rem;
box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
border: 1px solid rgba(255, 255, 255, 0.2);
}
#display {
font-size: 4.5rem;
font-family: 'Courier New', monospace;
font-weight: bold;
color: #fff;
text-shadow: 0 0 20px rgba(255, 255, 255, 0.8);
margin-bottom: 2rem;
letter-spacing: 0.1em;
min-height: 5rem;
display: flex;
align-items: center;
justify-content: center;
}
.buttons {
display: flex;
gap: 1rem;
flex-wrap: wrap;
justify-content: center;
}
button {
padding: 1rem 2rem;
font-size: 1.2rem;
font-weight: bold;
border: none;
border-radius: 12px;
cursor: pointer;
transition: all 0.3s ease;
text-transform: uppercase;
letter-spacing: 0.5px;
min-width: 120px;
}
button:not(:disabled):hover {
transform: translateY(-2px);
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.3);
}
button:not(:disabled):active {
transform: translateY(0);
}
#start {
background: linear-gradient(135deg, #4CAF50, #45a049);
color: white;
}
#pause {
background: linear-gradient(135deg, #FF9800, #e68900);
color: white;
}
#stop {
background: linear-gradient(135deg, #f44336, #d32f2f);
color: white;
}
#reset {
background: linear-gradient(135deg, #2196F3, #1976D2);
color: white;
}
button:disabled {
opacity: 0.5;
cursor: not-allowed;
transform: none !important;
}
@media (max-width: 600px) {
.container {
padding: 2rem 1rem;
margin: 1rem;
}
#display {
font-size: 3rem;
}
button {
padding: 0.8rem 1.5rem;
font-size: 1rem;
min-width: 100px;
}
}
</style>
</head>
<body>
<div class="container">
<div id="display">00:00:00.00</div>
<div class="buttons">
<button id="start">Start</button>
<button id="pause">Pause</button>
<button id="stop">Stop</button>
<button id="reset">Reset</button>
</div>
</div>
<script>
const display = document.getElementById('display');
const startBtn = document.getElementById('start');
const pauseBtn = document.getElementById('pause');
const stopBtn = document.getElementById('stop');
const resetBtn = document.getElementById('reset');
let startTime = 0;
let elapsedTime = 0;
let timerInterval;
let isRunning = false;
function updateDisplay() {
let time = elapsedTime;
if (isRunning) {
time += Date.now() - startTime;
}
const centiseconds = Math.floor(time / 10) % 100;
const seconds = Math.floor(time / 1000) % 60;
const minutes = Math.floor(time / 60000) % 60;
const hours = Math.floor(time / 3600000);
display.textContent = `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}.${centiseconds.toString().padStart(2, '0')}`;
}
function startTimer() {
if (isRunning) return;
startTime = Date.now() - elapsedTime;
timerInterval = setInterval(updateDisplay, 10);
isRunning = true;
startBtn.disabled = true;
resetBtn.disabled = true;
pauseBtn.disabled = false;
stopBtn.disabled = false;
}
function pauseTimer() {
if (!isRunning) return;
elapsedTime += Date.now() - startTime;
clearInterval(timerInterval);
isRunning = false;
startBtn.disabled = false;
resetBtn.disabled = false;
pauseBtn.disabled = true;
stopBtn.disabled = true;
updateDisplay();
}
function stopTimer() {
elapsedTime = 0;
clearInterval(timerInterval);
isRunning = false;
updateDisplay();
startBtn.disabled = false;
resetBtn.disabled = false;
pauseBtn.disabled = true;
stopBtn.disabled = true;
}
function resetTimer() {
elapsedTime = 0;
clearInterval(timerInterval);
isRunning = false;
updateDisplay();
startBtn.disabled = false;
resetBtn.disabled = false;
pauseBtn.disabled = true;
stopBtn.disabled = true;
}
// Event listeners
startBtn.addEventListener('click', startTimer);
pauseBtn.addEventListener('click', pauseTimer);
stopBtn.addEventListener('click', stopTimer);
resetBtn.addEventListener('click', resetTimer);
// Initial state
pauseBtn.disabled = true;
stopBtn.disabled = true;
updateDisplay();
</script>
</body>
</html>
This is a fully functional, responsive web-based stopwatch.
Features:
- Display: Shows time in
HH:MM:SS.CC format (hours, minutes, seconds, centiseconds) with high precision (~10ms updates).
- Start: Starts or resumes the timer from the current elapsed time.
- Pause: Pauses the timer, preserving the current time.
- Stop: Stops the timer and resets it to
00:00:00.00.
- Reset: Resets the timer to
00:00:00.00 (stops if running or paused).
- Button States: Buttons are dynamically enabled/disabled for intuitive UX (e.g., Pause/Stop only when running).
- Styling: Modern glassmorphism design, responsive for mobile/desktop, hover effects, gradients.
Copy-paste into an HTML file and open in a browser to use!