You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<h1> Click start to change the background color every second</h1>
37
+
38
+
<divclass="buttons">
39
+
<buttonid="start">Start</button>
40
+
<buttonid="stop">Stop</button>
41
+
</div>
42
+
</body>
43
+
44
+
<script>
45
+
46
+
// Generate random colors
47
+
48
+
constrandomColor=function(){
49
+
consthex="0123456789ABCDEF"
50
+
letcolor='#'
51
+
for(leti=0;i<6;i++){
52
+
color+=hex[Math.floor(Math.random()*16)]
53
+
}
54
+
returncolor;
55
+
};
56
+
57
+
letintervalID;
58
+
59
+
conststartColor=function(){
60
+
61
+
if(!intervalID){
62
+
intervalID=setInterval(changeBackground,1000)
63
+
}
64
+
65
+
functionchangeBackground(){
66
+
document.body.style.backgroundColor=randomColor()
67
+
}
68
+
}
69
+
70
+
conststopColor=function(){
71
+
clearInterval(intervalID)
72
+
intervalID=null;// this line is just clearing the value of intervalID as it was overriding every time when button was clicked. This makes the code more clear and shows professionalism.
0 commit comments