Skip to content

Instantly share code, notes, and snippets.

@mreis1
Created January 10, 2024 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mreis1/dbc2b0719b41710b2d8a15733fbe651e to your computer and use it in GitHub Desktop.
Save mreis1/dbc2b0719b41710b2d8a15733fbe651e to your computer and use it in GitHub Desktop.
Angular (NG): Snippet to display a count down in seconds
Time Left: {{countDownSecondsLeft}}
// @todo: Somewhere in your code, start the count down by doing: _countDownInit(() => { /* code to be executed on countdown complete */ })
// region countdown
private readonly COUNT_DOWN_SECS = 3;
private _countDownId: any;
countDownSecondsLeft = this.COUNT_DOWN_SECS;
private _countDownClear() {
clearInterval(this._countDownId);
this._countDownId = null;
}
private _countDownInit(cb: () => void) {
this._countDownClear();
this.countDownSecondsLeft = this.COUNT_DOWN_SECS;
this._countDownId = setInterval(() => cb(), 1000);
}
private _countDownTick() {
const v = --this.countDownSecondsLeft;
if (v === 0) {
this.confirm();
}
}
// endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment