-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent5.html
More file actions
49 lines (43 loc) · 1.17 KB
/
event5.html
File metadata and controls
49 lines (43 loc) · 1.17 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> Asynchronous js</title>
</head>
<body >
<h2> Start should changne the background color every second </h2>
<button id="start">Start</button>
<button id="stop">Stop</button>
</body>
<script>
//setInterval ,clearInterval
// for creating 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;
}
let intervalId;
const startChangeColor = function(){
if(!intervalId){
intervalId= setInterval(changeBgColor, 1000);
}
function changeBgColor(){
document.body.style.backgroundColor =randomColor();
}
};
const stopChangeColor = function(){
clearInterval(intervalId);
intervalId =null;
console.log("bc stopped")
};
document.querySelector('#start').
addEventListener('click', startChangeColor);
document.querySelector('#stop').
addEventListener('click', stopChangeColor);
</script>
</html>