1+ const buttonPlay = document . getElementById ( 'buttonPlay' ) ;
2+ const playIcon = document . getElementById ( 'playIcon' ) ;
3+ const buttonReset = document . getElementById ( 'buttonReset' ) ;
4+ const timeLeftDOM = document . getElementById ( 'timeLeft' ) ;
5+ const labelSessionBreak = document . getElementById ( 'labelSessionBreak' ) ;
6+ const sessionLengthDOM = document . getElementById ( 'sessionLength' ) ;
7+ const breakLengthDOM = document . getElementById ( 'breakLength' ) ;
8+ const sessionDecrement = document . getElementById ( 'sessionDecrement' ) ;
9+ const sessionIncrement = document . getElementById ( 'sessionIncrement' ) ;
10+ const breakDecrement = document . getElementById ( 'breakDecrement' ) ;
11+ const breakIncrement = document . getElementById ( 'breakIncrement' ) ;
12+
13+
14+ const arrayTime = timeLeftDOM . innerText . split ( ":" ) ;
15+ let timeLeft = parseInt ( arrayTime [ 0 ] * 60 ) + parseInt ( arrayTime [ 1 ] ) ;
16+ let playIsClicked = true ;
17+ let isSession = false ;
18+ let breakLength = 5 * 60 ;
19+ let timeLength = 25 * 60 ;
20+
21+ function convertSeconds ( seconds ) {
22+ return {
23+ minutes : Math . floor ( seconds / 60 ) ,
24+ seconds : seconds % 60
25+ }
26+ }
27+
28+ let interval ;
29+
30+ buttonPlay . addEventListener ( 'click' , ( ) => {
31+
32+ if ( playIsClicked ) {
33+
34+ if ( interval ) {
35+ clearInterval ( interval )
36+ }
37+ interval = setInterval ( handleTime , 1000 ) ;
38+
39+
40+ playIcon . classList . remove ( 'fa-play' ) ;
41+ playIcon . classList . add ( 'fa-pause' ) ;
42+
43+ function handleTime ( ) {
44+
45+ if ( timeLeft <= 0 ) {
46+
47+ if ( isSession ) {
48+ labelSessionBreak . innerText = "Session" ;
49+ timeLeft = timeLength ;
50+ }
51+
52+
53+ else {
54+ labelSessionBreak . innerText = "Break" ;
55+ timeLeft = breakLength ;
56+
57+
58+ }
59+ isSession = ! isSession ;
60+ }
61+
62+
63+ else if ( playIsClicked ) {
64+ clearInterval ( interval ) ;
65+ }
66+
67+
68+ else {
69+ timeLeft -- ;
70+ const minutesAndSeconds = convertSeconds ( timeLeft ) ;
71+ timeLeftDOM . innerText = `${ ( '0' + minutesAndSeconds . minutes ) . slice ( - 2 ) } :${ ( '0' + minutesAndSeconds . seconds ) . slice ( - 2 ) } ` ;
72+ }
73+ }
74+ }
75+
76+
77+ else {
78+
79+ playIcon . classList . add ( 'fa-play' ) ;
80+ playIcon . classList . remove ( 'fa-pause' ) ;
81+ }
82+ playIsClicked = ! playIsClicked ;
83+ } ) ;
84+
85+
86+ buttonReset . addEventListener ( 'click' , ( ) => {
87+ breakLength = 5 * 60 ;
88+ timeLength = 25 * 60 ;
89+ timeLeft = timeLength ;
90+ breakLengthDOM . innerText = "5" ;
91+ sessionLengthDOM . innerText = "25" ;
92+ timeLeftDOM . innerText = "25:00" ;
93+ if ( ! playIsClicked ) {
94+ buttonPlay . click ( ) ;
95+ }
96+ } ) ;
97+
98+
99+ function handleLengthButton ( lengthValue , htmlElement , isAddition , isBreakLength ) {
100+ let result = 1 ;
101+ if ( isAddition ) {
102+ result = ++ lengthValue ;
103+ htmlElement . innerText = result ;
104+ } else {
105+ if ( lengthValue != 1 ) {
106+ result = -- lengthValue ;
107+ htmlElement . innerText = result ;
108+ }
109+ }
110+ if ( ! playIsClicked ) {
111+ buttonPlay . click ( ) ;
112+ }
113+ let resultSeconds = result * 60 ;
114+ if ( ! isBreakLength ) {
115+ timeLength = resultSeconds ;
116+
117+ if ( labelSessionBreak . innerText === 'SESSION' ) {
118+ timeLeftDOM . innerText = ( '0' + result ) . slice ( - 2 ) + ":00" ;
119+ timeLeft = resultSeconds ;
120+ }
121+ } else {
122+ breakLength = resultSeconds ;
123+
124+ if ( labelSessionBreak . innerText === 'BREAK' ) {
125+ timeLeftDOM . innerText = ( '0' + result ) . slice ( - 2 ) + ":00" ;
126+ timeLeft = resultSeconds ;
127+ }
128+ }
129+ return resultSeconds ;
130+ }
131+ sessionDecrement . addEventListener ( 'click' , ( ) => {
132+ handleLengthButton ( parseInt ( sessionLengthDOM . innerText ) , sessionLengthDOM , false , false ) ;
133+ } ) ;
134+ sessionIncrement . addEventListener ( 'click' , ( ) => {
135+ handleLengthButton ( parseInt ( sessionLengthDOM . innerText ) , sessionLengthDOM , true , false ) ;
136+ } ) ;
137+ breakDecrement . addEventListener ( 'click' , ( ) => {
138+ breakLength = handleLengthButton ( parseInt ( breakLengthDOM . innerText ) , breakLengthDOM , false , true ) ;
139+ } ) ;
140+ breakIncrement . addEventListener ( 'click' , ( ) => {
141+ breakLength = handleLengthButton ( parseInt ( breakLengthDOM . innerText ) , breakLengthDOM , true , true ) ;
142+ } ) ;
0 commit comments