11let alarmInterval ;
2+ let colorInterval ;
23
34function setAlarm ( ) {
45 clearInterval ( alarmInterval ) ;
5- let timeRemaining = document . getElementById ( "alarmSet" ) ;
6- const display = document . getElementById ( "timeRemaining" ) ;
6+ const timeRemaining = document . querySelector ( "#alarmSet" ) ;
7+ const display = document . querySelector ( "#timeRemaining" ) ;
8+ if ( ! timeRemaining || ! display ) return ;
79 //what ever we input is turned into seconds
810 let totalSeconds = parseInt ( timeRemaining . value ) ;
911
@@ -30,6 +32,49 @@ function setAlarm() {
3032 }
3133 } , 1000 ) ;
3234}
35+ //add color changing background when alarm reaches 0 until stopped.
36+ //function from color changing assignment 10 year ago!
37+ function makeRandomColor ( ) {
38+ var colorOptions = '0123456789ABCDEF' ;
39+ var newColor = '#' ;
40+ //repeat 6 times to generate 6 random hex digits from the 16 characters in colorOptions
41+ for ( var i = 0 ; i < 6 ; i ++ ) {
42+ //picks random numbers from colorOptions and appends it to the # to make a new color.
43+ newColor += colorOptions [ Math . floor ( Math . random ( ) * 16 ) ] ;
44+ }
45+ return newColor ;
46+ }
47+ //end color changing
48+
49+ //keep the existing code below and call the setup
50+ //set the background color transition
51+ document . body . style . transition = "background-color 0.7s ease" ;
52+ // call the entire page
53+ window . addEventListener ( "load" , function ( ) {
54+ // attach the alarm sound to the make Random color so it starts changing when the alarm starts.
55+ audio . addEventListener ( "play" , ( ) => {
56+ // stops any previous color changing intervals
57+ clearInterval ( colorInterval ) ;
58+ //starts a new repeating timer and saves it's ID in colorInterval. setInterval runs every 2seconds
59+ colorInterval = setInterval ( ( ) => {
60+ //style the background and make it important! so it over-rides the style.css
61+ document . body . style . setProperty (
62+ "background-color" ,
63+ makeRandomColor ( ) ,
64+ "important"
65+ ) ;
66+ //closes setInterval after 2 seconds
67+ } , 1000 ) ;
68+ } ) ;
69+
70+ audio . addEventListener ( "pause" , ( ) => {
71+ clearInterval ( colorInterval ) ;
72+ document . body . style . backgroundColor = "" ;
73+ } ) ;
74+ } ) ;
75+ //not sure that this is helping. The function works without it.
76+ window . setAlarm = setAlarm ;
77+ //end linking
3378
3479// DO NOT EDIT BELOW HERE
3580
0 commit comments