⚡
Pomodoro Timer ⏱️
Intermediate
155 XP
65 min
Lesson Content
Pomodoro Timer ⏱️
Build a productivity timer that helps you focus. This uses setInterval and time management!
Timer Logic
let timeLeft = 25 * 60; // 25 minutes in seconds
let isRunning = false;
let intervalId = null;
function startTimer() {
if (!isRunning) {
isRunning = true;
intervalId = setInterval(() => {
timeLeft--;
if (timeLeft <= 0) {
stopTimer();
alert('Time is up!');
}
updateDisplay();
}, 1000);
}
}
function stopTimer() {
clearInterval(intervalId);
isRunning = false;
}Example Code
Create a countdown timer
let timeLeft = 5; // 5 seconds for testing
let intervalId = null;
function startTimer() {
intervalId = setInterval(() => {
timeLeft--;
console.log('Time left:', timeLeft);
if (timeLeft <= 0) {
clearInterval(intervalId);
console.log('Timer finished!');
}
}, 1000);
}
function resetTimer() {
clearInterval(intervalId);
timeLeft = 5;
}
startTimer();Expected Output:
Countdown from 5 to 0
Study Tips
- •Read the theory content thoroughly before practicing
- •Review the example code to understand key concepts
- •Proceed to the Practice tab when you're ready to code