@@ -69,6 +69,8 @@ function main() {
6969 }
7070 }
7171
72+
73+
7274 /*
7375 Params: db object
7476 Function initialises addNewTracker() function by clicking the add button (id="add-button")
@@ -142,10 +144,11 @@ function main() {
142144
143145
144146 function addCard ( id , title , c_time , db ) {
147+
145148 let card = document . getElementById ( "counter_tmpl" ) . content . firstElementChild . cloneNode ( true ) ;
146- console . debug ( "Calculated card ID " , ( "db_id" + id ) ) ;
149+ // console.debug("Calculated card ID ", ("db_id" + id));
147150 card . id = "db_id" + id ;
148- console . debug ( "Creating card ID " , card . id ) ;
151+ // console.debug("Creating card ID ", card.id);
149152
150153 let counter = 0 ;
151154 let subtotal_time = c_time ;
@@ -168,6 +171,25 @@ function main() {
168171 let timer_running = false ;
169172 let intervalId = null ;
170173
174+ let hide_time = 0 ;
175+ let hide_counter_state = 0 ;
176+ let hide_subtotal_time_state = 0 ;
177+
178+ let on_visibility_change = ( ) => {
179+ if ( document . hidden ) {
180+ hide_time = Date . now ( ) ;
181+ hide_counter_state = counter ;
182+ hide_subtotal_time_state = subtotal_time ;
183+ }
184+ else if ( timer_running ) {
185+ let elapsed_sec = Math . trunc ( ( Date . now ( ) - hide_time ) / 1000 ) ;
186+ counter += elapsed_sec ;
187+ subtotal_time += elapsed_sec ;
188+ }
189+ }
190+
191+ document . addEventListener ( "visibilitychange" , on_visibility_change ) ;
192+
171193 cb_start_pause . onclick = ( ) => {
172194 if ( timer_running === false ) {
173195 timer_running = true ;
@@ -213,6 +235,7 @@ function main() {
213235 }
214236
215237 cb_remove . onclick = ( ) => {
238+ document . removeEventListener ( "visibilitychange" , on_visibility_change ) ;
216239 clearInterval ( intervalId ) ;
217240 removeTracker ( id , db ) ;
218241 }
0 commit comments