-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
39 lines (33 loc) · 1.19 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
// Generating Random Colors
const randomColor = function () {
const hex = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += hex[Math.floor(Math.random() * 16)];
}
return color;
};
// Variable to store interval ID
let intervalId;
// Selecting the hex code display element
const hexCodeElement = document.querySelector('#hexCode');
// Function to change background color every second
const startChangingColor = function () {
function changeBgColor() {
const newColor = randomColor(); // Generate a new random color
document.body.style.backgroundColor = newColor; // Set the background color
hexCodeElement.textContent = 'Hex Code : ' + newColor; // Show the hex code
}
// Set interval only if not already set
if (!intervalId) {
intervalId = setInterval(changeBgColor, 1000);
}
};
// Function to stop changing color
const stopChangingColor = function () {
clearInterval(intervalId);
intervalId = null;
};
// Add event listeners to buttons
document.querySelector('#start').addEventListener('click', startChangingColor);
document.querySelector('#stop').addEventListener('click', stopChangingColor);