-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
69 lines (61 loc) · 2.06 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
let timer;
let isRunning = false;
let minutes = 25;
let seconds = 0;
const startButton = document.getElementById('start');
const pauseButton = document.getElementById('pause');
const resetButton = document.getElementById('reset');
const minutesDisplay = document.getElementById('minutes');
const secondsDisplay = document.getElementById('seconds');
function updateDisplay() {
minutesDisplay.textContent = String(minutes).padStart(2, '0');
secondsDisplay.textContent = String(seconds).padStart(2, '0');
if (isRunning) {
updateTabTitle(); // Update the tab title only if the timer is running
}
}
function updateTabTitle() {
if (isRunning) {
document.title = `Time Left: ${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
} else {
document.title = "Pomodoro Timer"; // Default title when the timer is not running
}
}
function startTimer() {
if (!isRunning) {
isRunning = true;
timer = setInterval(() => {
if (seconds === 0) {
if (minutes === 0) {
clearInterval(timer);
isRunning = false;
alert("Time's up!");
document.title = "Pomodoro Timer"; // Reset the title when the timer ends
} else {
minutes--;
seconds = 59;
}
} else {
seconds--;
}
updateDisplay();
}, 1000);
}
}
function stopTimer() {
clearInterval(timer);
isRunning = false;
document.title = "Pomodoro Timer"; // Reset the title when the timer is stopped
}
function resetTimer() {
clearInterval(timer);
isRunning = false;
minutes = 25;
seconds = 0;
updateDisplay();
document.title = "Pomodoro Timer"; // Reset the title when the timer is reset
}
startButton.addEventListener('click', startTimer);
pauseButton.addEventListener('click', stopTimer);
resetButton.addEventListener('click', resetTimer);
updateDisplay(); // Initialize the display with the default title