Skip to content

Commit 3220be0

Browse files
committed
Unlimited Colors changing in the background Project
1 parent 1a6f494 commit 3220be0

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// generate a random color
2+
3+
const randomColor = function(){
4+
const hex = "0123456789ABCDEF"
5+
let color = '#'
6+
for(let i = 0; i<6; i++){
7+
color += hex[Math.floor(Math.random() * 16)];
8+
}
9+
return color;
10+
};
11+
let intervalId;
12+
const startChangingColor = function(){
13+
if(!intervalId){
14+
intervalId = setInterval(changeBgColor, 1000 );
15+
}
16+
function changeBgColor(){
17+
document.body.style.backgroundColor = randomColor();
18+
}
19+
};
20+
21+
const stopChangingColor = function(){
22+
clearInterval(intervalId);
23+
intervalId = null;
24+
};
25+
26+
document.querySelector('#start').addEventListener('click', startChangingColor);
27+
28+
document.querySelector('#stop').addEventListener('click', stopChangingColor);

0 commit comments

Comments
 (0)