### Title: Building a Pomodoro Timer with HTML, CSS, and JavaScript
### Description:
In this article, we will guide you through the process of building a Pomodoro timer using HTML, CSS, and JavaScript. A Pomodoro timer is a technique used to improve focus and productivity, typically involving intervals of work followed by short breaks. We'll cover how to structure your HTML for the interface, style it with CSS for a clean look, and use JavaScript to manage the timing and functionality.
### Content:
## Introduction to Pomodoro Timer
A Pomodoro timer is a popular method for increasing productivity and concentration. It involves working in focused intervals (typically 25 minutes) followed by short breaks (usually 5 minutes). This cycle repeats four times before taking a longer break (15-30 minutes). By using a timer to break down work into manageable chunks, Pomodoros can help you stay focused and motivated.
## Setting Up Your Project
First, let's set up our project directory and files. We'll create three main files: `index.html`, `styles.css`, and `script.js`.
### index.html
This file will contain the basic structure of our web page.
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pomodoro Timer</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="timer-container">
<h1>Pomodoro Timer</h1>
<div id="time-display"></div>
<button id="start-stop">Start/Stop</button>
<button id="reset">Reset</button>
</div>
<script src="script.js"></script>
</body>
</html>
```
### styles.css
Here, we will style our timer.
```css
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f4f4f9;
}
.timer-container {
text-align: center;
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
#time-display {
margin-bottom: 20px;
}
button {
padding: 10px 20px;
margin: 5px;
cursor: pointer;
}
```
### script.js
Finally, we'll write the JavaScript code to handle the timer logic.
```javascript
let intervalId;
let timeLeft = 25 * 60; // 25 minutes in seconds
document.getElementById('start-stop').addEventListener('click', function() {
if (intervalId === undefined) {
startTimer();
} else {
clearInterval(intervalId);
document.getElementById('start-stop').textContent = 'Start/Stop';
}
});
function startTimer() {
document.getElementById('start-stop').textContent = 'Stop';
intervalId = setInterval(() => {
timeLeft--;
const minutes = Math.floor(timeLeft / 60);
const seconds = timeLeft % 60;
document.getElementById('time-display').textContent = `${minutes < 10 ? '0' + minutes : minutes}:${seconds < 10 ? '0' + seconds : seconds}`;
if (timeLeft <= 0) {
clearInterval(intervalId);
document.getElementById('start-stop').textContent = 'Start/Stop';
alert('Your break is ready!');
}
}, 1000);
}
document.getElementById('reset').addEventListener('click', function() {
timeLeft = 25 * 60; // Reset timer
document.getElementById('time-display').textContent = '00:00';
document.getElementById('start-stop').textContent = 'Start/Stop';
});
```
## Conclusion
With these steps, you now have a functional Pomodoro timer built using HTML, CSS, and JavaScript. Feel free to customize the colors, fonts, and layout to fit your preferences. This timer can be a great tool for enhancing your productivity and maintaining a healthy work-life balance.