-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
49 lines (33 loc) · 891 Bytes
/
script.js
File metadata and controls
49 lines (33 loc) · 891 Bytes
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
const start = document.querySelector('#start');
const stop = document.querySelector('#stop');
const body = document.querySelector('body')
let startInterval;
// Hex color code generator.
let hex = '0123456789abcdef';
const colorId = function(){
let colorHex = "#";
for(let i = 0; i < 6; i++){
colorHex+= hex[Math.floor(Math.random()*16)];
}
return colorHex
};
// color setter
const changeColor = function(){
body.style.backgroundColor = colorId();
}
let intervalId;
// color change start
startInterval = function(){
if(!intervalId){
intervalId = setInterval(changeColor, 1000);
}
};
// start button click
start.addEventListener('click', startInterval);
// color change stop
let stopInterval = function(){
clearInterval(intervalId);
intervalId = null;
};
// stop button click
stop.addEventListener('click', stopInterval);