-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (39 loc) · 1 KB
/
index.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
const colors = [
'#FFFFFF',
'#2196F3',
'#4CAF50',
'#FF9800',
'#009688',
'#795548',
];
const body = document.querySelector('body');
const btnStart = document.querySelector('button[data-action="start"]');
const btnEnd = document.querySelector('button[data-action="stop"]');
const colorLength = colors.length - 1;
const colorChange = {
intervalId: null,
isActive: false,
startColorChanges() {
if (this.isActive) {
return;
}
this.isActive = true;
this.intervalId = setInterval(() => {
const randomIntegerFromInterval = (min, max) => {
return Math.floor(Math.random() * (max - min + 1) + min);
};
body.style.backgroundColor =
colors[randomIntegerFromInterval(0, colorLength)];
}, 1000);
},
stopColorChanges() {
clearInterval(this.intervalId);
this.isActive = false;
},
};
btnStart.addEventListener('click', () => {
colorChange.startColorChanges();
});
btnEnd.addEventListener('click', () => {
colorChange.stopColorChanges();
});