9
9
// make the sound happen by using `playAlarm()`.
10
10
// You can stop the alarm sound by pressing the `Stop Alarm` button.
11
11
12
- const alarmInputArea = document . querySelector ( "#alarmSet" ) ;
12
+ const alarmInputArea = document . querySelector ( "#alarmSet" ) ; // get access to InputArea
13
13
console . log ( alarmInputArea , "<---- InputArea" ) ;
14
14
15
15
const setAlarmButton = document . querySelector ( "#set" ) ;
@@ -24,64 +24,65 @@ console.log(timeRemainInfo, "<------Remain Time");
24
24
const backgroundColor = document . querySelector ( "html" ) ;
25
25
console . log ( backgroundColor , "<------Background Color" ) ;
26
26
27
- let intervalId ;
28
- let inputInfo = 0 ;
27
+ let intervalId ; // declare variable
28
+ let inputInfo = 0 ; // declare 'inputInfo' as an 'intervalId' global
29
29
30
30
function timeFormat ( time ) {
31
+ // revert number into min and sec format function
31
32
const mins = String ( Math . floor ( time / 60 ) ) . padStart ( 2 , "0" ) ;
32
33
const seconds = String ( time % 60 ) . padStart ( 2 , "0" ) ;
33
34
timeRemainInfo . textContent = `Time Remaining: ${ mins } :${ seconds } ` ;
34
35
}
35
36
36
37
function setAlarm ( ) {
37
- inputInfo = Number ( alarmInputArea . value ) ;
38
+ inputInfo = Number ( alarmInputArea . value ) ; // convert input into number
38
39
if ( inputInfo < 1 || isNaN ( inputInfo ) ) {
40
+ // to sift invalid input
39
41
console . log ( "You need to input a value in seconds!" ) ;
40
42
window . alert ( "You need to input a value in seconds!" ) ;
41
43
return ;
42
44
}
43
- alarmInputArea . value = "" ;
44
- timeFormat ( inputInfo ) ;
45
+ alarmInputArea . value = "" ; // clean input area when data assigned to 'inputInfo'
46
+ timeFormat ( inputInfo ) ; // set format input to min and sec
45
47
46
48
intervalId = setInterval ( function ( ) {
47
- inputInfo -- ;
49
+ //function to set interval for
50
+ inputInfo -- ; // countdown the input by 1 sec by iterat.
48
51
if ( inputInfo === 0 ) {
52
+ // if input equal 0, set alarm signal and flashing
49
53
clearInterval ( intervalId ) ;
50
54
timeFormat ( 0 ) ;
51
55
window . playAlarm ( ) ;
52
56
changeBackgroundColorFlashing ( ) ;
53
57
} else {
58
+ // if input not equal 0, just continue count down
54
59
timeFormat ( inputInfo ) ;
55
60
}
56
61
} , 1000 ) ;
57
62
}
58
63
59
- //setAlarmButton.addEventListener("click", function setAlarm() {
60
- //console.log("click event is firing...");
61
- //});
62
- let flashColor = [ "yellow" , "pink" , "lightgrey" , "green" ] ;
63
- let flashIntervalId ;
64
+ let flashColor = [ "yellow" , "pink" , "lightgrey" , "green" ] ; // array for flashing
65
+ let flashIntervalId ; // declaring globally variable
64
66
let colorIndex = 0 ;
65
67
66
68
setStopButton . addEventListener ( "click" , function stopFlashing ( ) {
67
69
console . log ( "click event is firing..." ) ;
68
- clearInterval ( flashIntervalId ) ;
69
- backgroundColor . style . backgroundColor = "" ;
70
+ clearInterval ( flashIntervalId ) ; // cleaning any previous possible intervals
71
+ backgroundColor . style . backgroundColor = "" ; // cleaning background to default
72
+ // option after clicking stop button
70
73
window . pauseAlarm ( ) ;
71
74
} ) ;
72
75
73
- //function changeBackgroundColor() {
74
- //backgroundColor.style.backgroundColor = "lightblue";
75
- //}
76
-
77
76
function changeBackgroundColorFlashing ( ) {
78
- if ( flashIntervalId ) clearInterval ( flashIntervalId ) ;
77
+ //function flashing colors
78
+ if ( flashIntervalId ) clearInterval ( flashIntervalId ) ; // every 1 sec
79
79
flashIntervalId = setInterval ( function ( ) {
80
80
backgroundColor . style . backgroundColor = flashColor [ colorIndex ] ;
81
- colorIndex = ( colorIndex + 1 ) % flashColor . length ;
81
+ colorIndex = ( colorIndex + 1 ) % flashColor . length ; // loop over flashColor array
82
82
} , 1000 ) ;
83
- //stopFlashing();
83
+ //stopFlashing(); //setTimeout(() => clearInterval(flashIntervalId), 1000);
84
84
}
85
+
85
86
window . playAlarm = playAlarm ;
86
87
window . pauseAlarm = pauseAlarm ;
87
88
0 commit comments