BCA Student Stories & UpdatesLive
JavaScript Timers: setTimeout, setInterval & Digital Clock / Countdown Engine
Timers in JavaScript enable time-driven execution such as digital clocks, exam countdowns, auto-save engines, and animated sequences. Because JavaScript is single-threaded, timers do not freeze the main thread; rather, they register callbacks with the browser Web API layer.
Day 33 Step-by-Step Practical Flow
🎯1. What You Will Learn Today
JavaScript Timers: setTimeout, setInterval & Digital Clock / Countdown Engine — Comprehensive Practical Guide
Har concept ko visual mental models, code anatomy aur practical industry tips ke sath samjhein.
Timers in JavaScript enable time-driven execution such as digital clocks, exam countdowns, auto-save engines, and animated sequences. Because JavaScript is single-threaded, timers do not freeze the main thread; rather, they register callbacks with the browser Web API layer.
"Think of setTimeout like setting an alarm on your phone: you set it for 10 minutes and continue working freely. When the alarm rings, your phone alerts you without stopping what you were doing."
👨🏫3. Teacher Demonstration Code
Demonstrating how to format live system time and manage countdown intervals safely.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: 'Segoe UI', monospace; background: #060c18; color: #fff; text-align: center; padding: 40px; }
.clock-card { max-width: 400px; margin: auto; background: #0c1328; border: 1px solid #1e293b; border-radius: 16px; padding: 24px; }
.display { font-size: 42px; font-weight: bold; color: #10b981; letter-spacing: 2px; margin: 20px 0; }
.btn-row { display: flex; gap: 8px; justify-content: center; }
button { padding: 10px 18px; border-radius: 8px; border: none; font-weight: bold; cursor: pointer; color: #fff; }
.btn-start { background: #2563eb; }
.btn-pause { background: #f59e0b; color: #000; }
.btn-reset { background: #ef4444; }
</style>
</head>
<body>
<div class="clock-card">
<h3>⏱️ Exam Countdown Engine</h3>
<div class="display" id="timeDisplay">05:00</div>
<div class="btn-row">
<button class="btn-start" id="startBtn">Start</button>
<button class="btn-pause" id="pauseBtn">Pause</button>
<button class="btn-reset" id="resetBtn">Reset</button>
</div>
</div>
<script>
let totalSeconds = 300; // 5 minutes
let timerId = null;
function renderDisplay() {
const mins = Math.floor(totalSeconds / 60);
const secs = totalSeconds % 60;
document.getElementById("timeDisplay").textContent =
String(mins).padStart(2, "0") + ":" + String(secs).padStart(2, "0");
}
document.getElementById("startBtn").addEventListener("click", () => {
if (timerId !== null) return; // Prevent stacking intervals
timerId = setInterval(() => {
if (totalSeconds > 0) {
totalSeconds--;
renderDisplay();
} else {
clearInterval(timerId);
timerId = null;
alert("⏰ Time is up! Examination submitted.");
}
}, 1000);
});
document.getElementById("pauseBtn").addEventListener("click", () => {
clearInterval(timerId);
timerId = null;
});
document.getElementById("resetBtn").addEventListener("click", () => {
clearInterval(timerId);
timerId = null;
totalSeconds = 300;
renderDisplay();
});
renderDisplay();
</script>
</body>
</html>2. Hands-On Practical Lab: Follow Instructions in VS Code
Code like a professional software engineer! Follow the step-by-step instructions below in Visual Studio Code (VS Code) on your PC, test your output locally, and push to GitHub.
Day 33 Practical Task Test Studio
Use String(seconds).padStart(2, "0") to ensure digits are formatted with leading zeros.
Ensure clearInterval(timerId) is called inside pause and reset handlers and set timerId = null.
The Stacking Multiple Intervals Bug
Tap or click on the exact line below that contains the error:
Click any code linelet timer = null;let seconds = 60; // Clicking this button 5 times starts 5 intervals!// Seconds decrease by 5 every second!document.getElementById("start").addEventListener("click", () => { timer = setInterval(() => { seconds--; console.log(seconds); }, 1000);});Senior Developer Forensic Analysis:
Each click to start creates a new setInterval without clearing the previous one. The previous interval handles continue running in background memory, accelerating the countdown and causing race conditions.
📝Independent Practice: Assignment
📄 index.htmlBCA Term-End Examination CBT Countdown Engine
- •Build a complete 60-minute examination timer interface with hours, minutes, and seconds display.
- •Change timer display color to Amber at 10 minutes remaining and pulsing Red at 1 minute remaining.
- •Add a warning popup modal when 5 minutes remain.
- •Auto-submit and lock the examination when the timer reaches 00:00:00.
Open Visual Studio Code on your PC (code .), create index.html, paste this complete step-by-step verified code, test in your browser, and push to your GitHub repo:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>BCA Day 33 - Exam Timer</title>
<style>
body { font-family: 'Segoe UI', monospace; background: #060c18; color: #fff; padding: 24px; text-align: center; }
.card { max-width: 480px; margin: auto; background: #0c1328; border: 1px solid #1e293b; padding: 24px; border-radius: 14px; }
.timer { font-size: 52px; font-weight: 800; color: #10b981; margin: 20px 0; transition: color 0.3s; }
.timer.warning { color: #f59e0b; }
.timer.danger { color: #ef4444; animation: pulse 1s infinite; }
@keyframes pulse { 0% { opacity: 1; } 50% { opacity: 0.5; } 100% { opacity: 1; } }
.btn-row { display: flex; gap: 8px; justify-content: center; }
button { padding: 12px 20px; font-weight: bold; border-radius: 8px; border: none; cursor: pointer; color: #fff; }
.start { background: #2563eb; }
.pause { background: #64748b; }
.reset { background: #dc2626; }
</style>
</head>
<body>
<div class="card">
<h2>🎓 BCSL-013 Lab Exam Timer</h2>
<div class="timer" id="timerDisplay">60:00</div>
<div class="btn-row">
<button class="start" id="startBtn">Start Exam</button>
<button class="pause" id="pauseBtn">Pause</button>
<button class="reset" id="resetBtn">Reset</button>
</div>
</div>
<script>
let remaining = 3600; // 60 mins
let timer = null;
const display = document.getElementById("timerDisplay");
function update() {
const m = Math.floor(remaining / 60);
const s = remaining % 60;
display.textContent = String(m).padStart(2, "0") + ":" + String(s).padStart(2, "0");
if (remaining <= 60) {
display.className = "timer danger";
} else if (remaining <= 600) {
display.className = "timer warning";
} else {
display.className = "timer";
}
}
document.getElementById("startBtn").addEventListener("click", () => {
if (timer) return;
timer = setInterval(() => {
if (remaining > 0) {
remaining--;
update();
} else {
clearInterval(timer);
timer = null;
alert("🚨 Examination Time Expired! Papers auto-submitted.");
}
}, 1000);
});
document.getElementById("pauseBtn").addEventListener("click", () => {
clearInterval(timer);
timer = null;
});
document.getElementById("resetBtn").addEventListener("click", () => {
clearInterval(timer);
timer = null;
remaining = 3600;
update();
});
update();
</script>
</body>
</html>🎓4. Viva Check: Mandatory Conceptual Test
Why is setTimeout(callback, 1000) not guaranteed to execute after exactly 1000.000 milliseconds?
5. Mandatory Step: Push to GitHub to Unlock Next Lesson
Target Repo: html-css-projects
Exact GitHub Setup Guide & Expected Structure
Day 33 RequirementsFollow these exact folder and file names to prevent verification errors.
index.html: Avoid spaces or custom names like Alan Turing..html or Index.HTML. Linux/GitHub servers are strictly case-sensitive.day-33/ for today so you don't overwrite previous days.main.You are welcome to read all notes, inspect code snippets, and practice all exercises of Day 33 freely! To officially submit your GitHub repository and unlock subsequent days in your portfolio, please complete Day 32: Persistent Task & To-Do List with LocalStorage CRUD Mechanics first.
Complete Day 32 First