-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
130 lines (102 loc) · 3.89 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const minutesDisplay = document.getElementById('minutes');
const secondsDisplay = document.getElementById('seconds');
const workPanel = document.getElementById('work');
const breakPanel = document.getElementById('break');
const startButton = document.getElementById('start');
const resetButton = document.getElementById('reset');
// Background toggles
let workBackgroundToggle = false;
let breakBackgroundToggle = false;
// Initial time settings
let intervalID;
let workTime = 25;
let breakTime = 5;
let isWorkTime = true; // flag to track if it's work time or break time
function updateDisplay(minutes, seconds) {
minutesDisplay.textContent = String(minutes).padStart(2, '0');
secondsDisplay.textContent = String(seconds).padStart(2, '0');
}
function preloadImages(...images) {
images.forEach((image) => {
const img = new Image(); // Creates a new HTMLImageElement instance
img.src = image; // Sets the source path which starts loading the image
});
}
// Call this function with the paths to your images
preloadImages(
'work_background_1.jpg',
'work_background_2.jpg',
'break_background_1.jpg',
'break_background_2.jpg'
);
function updateBackground() {
// Clear previous background classes
document.body.classList.remove('work-time-1', 'work-time-2', 'break-time-1', 'break-time-2');
// Determine the new background class based on the toggle state
const newClass = isWorkTime
? (workBackgroundToggle ? 'work-time-2' : 'work-time-1')
: (breakBackgroundToggle ? 'break-time-2' : 'break-time-1');
// Add the new class
document.body.classList.add(newClass);
// Toggle the state for next time
if (isWorkTime) {
workBackgroundToggle = !workBackgroundToggle;
} else {
breakBackgroundToggle = !breakBackgroundToggle;
}
}
function startTimer() {
const endTime = Date.now() + (isWorkTime ? workTime : breakTime) * 60000; // End time in milliseconds
updateSessionIndicator();
startButton.style.display = "none";
resetButton.style.display = "block";
intervalID = setInterval(() => {
const currentTime = Date.now();
const timeLeft = endTime - currentTime; // Time left in milliseconds
if (timeLeft <= 0) {
clearInterval(intervalID);
isWorkTime = !isWorkTime; // Toggle between work and break time
updateBackground(); // Update background before starting the next session
startTimer(); // Automatically start the next session
return;
}
const minutes = Math.floor(timeLeft / 60000);
const seconds = Math.floor((timeLeft % 60000) / 1000);
updateDisplay(minutes, seconds);
}, 1000);
updateBackground();
}
// Function to update the session indicator
function updateSessionIndicator() {
if (isWorkTime) {
workPanel.classList.add('active-session');
breakPanel.classList.remove('active-session');
} else {
breakPanel.classList.add('active-session');
workPanel.classList.remove('active-session');
}
}
// Function to reset the timer
function resetTimer() {
clearInterval(intervalID);
isWorkTime = true;
updateDisplay(workTime, 0);
startButton.style.display = "block"; // Show the start button
resetButton.style.display = "none"; // Hide the reset button
updateBackground();
}
// Event listeners for buttons
startButton.addEventListener('click', startTimer);
resetButton.addEventListener('click', (event) => {
event.preventDefault(); // Prevent default anchor behavior
resetTimer();
});
window.onload = function () {
preloadImages(
'work_background_1.jpg',
'work_background_2.jpg',
'break_background_1.jpg',
'break_background_2.jpg'
);
resetTimer();
};