-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path32_async_Project1.html
More file actions
57 lines (43 loc) · 1.3 KB
/
32_async_Project1.html
File metadata and controls
57 lines (43 loc) · 1.3 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Unlimited Colors</title>
</head>
<body style="background-color: black; color: rgb(182, 217, 248);">
<div class="main">
<h1>Start should change the background color every second</h1>
<button id="start">START</button>
<button id="stop">STOP</button>
</div>
</body>
<script>
const start = document.querySelector('#start')
const stop = document.querySelector('#stop')
const body = document.querySelector('body')
function getRandomColor() {
const letter = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letter[Math.floor(Math.random() * 16)];
}
return color;
}
let allColors;
const changingColor = function(){
if(!allColors){
allColors = setInterval(myCol, 600)
}
function myCol(){
body.style.backgroundColor = getRandomColor()
}
}
const stopColor = function(){
clearInterval(allColors);
allColors = null
}
start.addEventListener('click', changingColor)
stop.addEventListener('click', stopColor)
</script>
</html>