Season 2 ka teesra project: ek stopwatch — Start/Stop, Lap aur Reset ke saath. Isse tum seekhoge ki setInterval se baar-baar kaam kaise chalta hai, aur milliseconds ko 00:00.00 me kaise badalte hain. Stop dabake dobara Start karoge to time wahin se aage badhega!
setInterval / clearInterval — har 10 ms par kaam chalana aur roknaDate.now() — asli time se ginti (sirf counter badhane se time galat hota hai)Math.floor aur %padStart(2, '0') — 5 ko "05" bananaprepend se list me upar jodnaHar step me "Editor me daalo" dabao — ya best: khud type karo, haath se likha code yaad rehta hai. ✍️
#time me 00:00.00 dikhega — minute : second . centisecond.<ol reversed> — numbered list jo ulti ginti me chalti hai, taaki sabse naya lap upar aur sabse bada number ho. <div class="watch">
<div id="time">00:00.00</div>
<div class="btns">
<button id="start">Start</button>
<button id="lap">Lap</button>
<button id="reset">Reset</button>
</div>
<ol id="laps" reversed></ol>
</div>
display: grid + place-items: center.text-align: center — time aur buttons beech me.body {
margin: 0;
min-height: 100vh;
display: grid;
place-items: center;
background: #0f172a;
font-family: sans-serif;
}
.watch {
width: 300px;
padding: 24px;
background: #1e293b;
border-radius: 24px;
text-align: center;
}
font: 46px monospace — monospace me har digit barabar chaudai ka hota hai, isliye time hilta nahi..btns { display: flex } aur flex: 1 — teeno buttons barabar chaude.#id se alag rang.#time {
padding: 20px 0;
color: #fff;
font: 46px monospace;
}
.btns { display: flex; gap: 8px; }
button {
flex: 1;
padding: 12px 0;
border: 0;
border-radius: 12px;
color: #fff;
font-size: 16px;
cursor: pointer;
}
#start { background: #22c55e; }
#lap { background: #6366f1; }
#reset { background: #ef4444; }
max-height: 150px + overflow: auto — zyada laps ho jayein to list scroll hogi, card badhega nahi.li par neeche halki line.#laps {
max-height: 150px;
margin: 16px 0 0;
padding-left: 28px;
overflow: auto;
color: #cbd5e1;
text-align: left;
}
#laps li { padding: 6px 0; border-bottom: 1px solid #334155; }
querySelector nahi likhna padega.elapsed = ab tak kitne ms ginti hui. startedAt = jab start dabaya. timer = interval ka id (null = ruka hua).const timeEl = document.querySelector('#time');
const startBtn = document.querySelector('#start');
const lapBtn = document.querySelector('#lap');
const resetBtn = document.querySelector('#reset');
const laps = document.querySelector('#laps');
let elapsed = 0;
let startedAt = 0;
let timer = null;
two(n) — padStart(2, '0') se 7 ko "07".% 60 aur % 100 se sirf bacha hua hissa lete hain.tick(): abhi ka time minus shuru ka time = elapsed. Phir screen par dikhao.function two(n) {
return String(n).padStart(2, '0');
}
function format(ms) {
const m = Math.floor(ms / 60000);
const s = Math.floor(ms / 1000) % 60;
const cs = Math.floor(ms / 10) % 100;
return `${two(m)}:${two(s)}.${two(cs)}`;
}
function tick() {
elapsed = Date.now() - startedAt;
timeEl.textContent = format(elapsed);
}
timer chal raha hai to rok do (clearInterval) aur button ko Start karo.startedAt = Date.now() - elapsed — itna peeche ka shuru maano ki purana time bacha rahe. Isse Stop ke baad Start karne par time wahin se aage badhta hai.setInterval(tick, 10) — har 10 ms par tick chalao.function toggle() {
if (timer) {
clearInterval(timer);
timer = null;
startBtn.textContent = 'Start';
} else {
startedAt = Date.now() - elapsed;
timer = setInterval(tick, 10);
startBtn.textContent = 'Stop';
}
}
lap(): sirf chalte stopwatch par. Naya li banao aur prepend se list ke upar lagao.reset(): interval roko, sab zero karo, button ko Start karo aur laps ki list saaf.function lap() {
if (!timer) return;
const li = document.createElement('li');
li.textContent = format(elapsed);
laps.prepend(li);
}
function reset() {
clearInterval(timer);
timer = null;
elapsed = 0;
timeEl.textContent = format(0);
startBtn.textContent = 'Start';
laps.innerHTML = '';
}
addEventListener — har ek apna function chalata hai.startBtn.addEventListener('click', toggle);
lapBtn.addEventListener('click', lap);
resetBtn.addEventListener('click', reset);
Naya day aate hi reel aayegi — @codeyantra follow karo 🔔