-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
43 lines (34 loc) · 1.21 KB
/
script.js
File metadata and controls
43 lines (34 loc) · 1.21 KB
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
function updateClock() {
const currentTime = new Date();
const hours = currentTime.getHours();
const minutes = currentTime.getMinutes();
const seconds = currentTime.getSeconds();
const ampm = hours >= 12 ? 'PM' : 'AM';
const formattedHours = hours % 12 || 12;
const formattedMinutes = minutes < 10 ? '0' + minutes : minutes;
const formattedSeconds = seconds < 10 ? '0' + seconds : seconds;
const formattedTime = `${formattedHours}:${formattedMinutes}:${formattedSeconds} ${ampm}`;
// Get the clock element and set its text content
const clockElement = document.getElementById('clock');
clockElement.textContent = formattedTime;
// Change background color
changeBackgroundColor();
}
function changeBackgroundColor() {
// Get a random color
const randomColor = getRandomColor();
// Apply the random color to the body background
document.body.style.backgroundColor = randomColor;
}
function getRandomColor() {
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
// Set up the interval for updating the clock
setInterval(updateClock, 1000);
// Initial call to updateClock
updateClock();