From ca410ce7422e70df9b55c912eca836d318e9be77 Mon Sep 17 00:00:00 2001 From: Chrystian Huot Date: Mon, 13 Jul 2020 08:25:38 -0400 Subject: [PATCH] Update clock on tab focus --- .../rdio-scanner/rdio-scanner.component.ts | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/client/src/app/components/rdio-scanner/rdio-scanner.component.ts b/client/src/app/components/rdio-scanner/rdio-scanner.component.ts index 76c24a2..9088ca8 100644 --- a/client/src/app/components/rdio-scanner/rdio-scanner.component.ts +++ b/client/src/app/components/rdio-scanner/rdio-scanner.component.ts @@ -129,7 +129,7 @@ export class AppRdioScannerComponent implements AfterViewInit, OnDestroy { private subscriptions: Subscription[] = []; - private visibilityChangeListener = () => this.ngDetectChanges(); + private visibilityChangeListener = () => this.syncClock(); constructor( private appRdioScannerService: AppRdioScannerService, @@ -224,15 +224,7 @@ export class AppRdioScannerComponent implements AfterViewInit, OnDestroy { // initialize the realtime clock - const setClock = () => { - this.clock = new Date(); - - this.clockRefresh = setTimeout(() => setClock(), (60 - this.clock.getSeconds()) * 1000); - - this.ngDetectChanges(); - }; - - setClock(); + this.syncClock(); // subscribe to events @@ -783,6 +775,18 @@ export class AppRdioScannerComponent implements AfterViewInit, OnDestroy { } } + private syncClock(): void { + this.clock = new Date(); + + if (this.clockRefresh) { + clearTimeout(this.clockRefresh); + } + + this.clockRefresh = setTimeout(() => this.syncClock(), (60 - this.clock.getSeconds()) * 1000); + + this.ngDetectChanges(); + }; + private transformCall(call: RdioScannerCall): RdioScannerCall { if (call && Array.isArray(this.config && this.config.systems)) { call.systemData = this.config?.systems.find((sys) => sys.id === call.system);