-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract countdown logic into dedicated component
- Loading branch information
Joao Leonardo Pereira
committed
Jul 19, 2024
1 parent
c450c74
commit 0c09e6a
Showing
6 changed files
with
86 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 1 addition & 3 deletions
4
src/app/components/countdown-timer/countdown-timer.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
<p> | ||
countdown-timer works! | ||
</p> | ||
<h2>{{timerLabel}}s</h2> |
51 changes: 48 additions & 3 deletions
51
src/app/components/countdown-timer/countdown-timer.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,59 @@ | ||
import { Component, OnInit } from '@angular/core'; | ||
import { Component, EventEmitter, Input, Output } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'app-countdown-timer', | ||
templateUrl: './countdown-timer.component.html', | ||
styleUrls: ['./countdown-timer.component.scss'], | ||
}) | ||
export class CountdownTimerComponent implements OnInit { | ||
export class CountdownTimerComponent { | ||
private timerRefreshInterval: any | ||
private _timerStartTime = 0 | ||
private _seconds = 0 | ||
@Input() set seconds(value: number) { | ||
this._seconds = value | ||
this.timerLabel = value | ||
this.stopTimer() | ||
} | ||
get seconds(): number { | ||
return this._seconds | ||
} | ||
|
||
@Output() timerEnd = new EventEmitter<void>(); | ||
|
||
timerLabel: number = 0; | ||
|
||
constructor() { } | ||
|
||
ngOnInit() {} | ||
public startTimer() { | ||
this.setupTimerInterval() | ||
} | ||
|
||
private stopTimer() { | ||
if(this.timerRefreshInterval) { | ||
clearInterval(this.timerRefreshInterval) | ||
} | ||
} | ||
|
||
private setupTimerInterval() { | ||
if(this.timerRefreshInterval) { | ||
clearInterval(this.timerRefreshInterval) | ||
} | ||
|
||
this.timerLabel = this.seconds // reset timer label | ||
this._timerStartTime = Date.now() // reset timer start time | ||
|
||
this.timerRefreshInterval = setInterval(() => { | ||
this.updateTimerLabel() | ||
if(this.timerLabel <= 0) { | ||
clearInterval(this.timerRefreshInterval) | ||
this.timerEnd.emit() | ||
} | ||
}, 250) // for precision purposes check every 250ms | ||
} | ||
|
||
private updateTimerLabel() { | ||
const elapsedTime = Math.ceil((Date.now() - this._timerStartTime)/1000) | ||
this.timerLabel = this.seconds - elapsedTime | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters