Reputation: 725
Scenario: Here i am looking for a count down timer where the user passes some seconds and user presses start or stop for this i am using ngx-countdown timer
requirements:
below is my code
<countdown [config]="{leftTime: timeData}" (event)="handleEvent($event)"></countdown>
`
})
export class AppComponent {
timeData = "120"
constructor(){
}
handleEvent(event){
console.log(event)
}
below is my stackblitz url
https://stackblitz.com/edit/ngx-countdown-setup-bwdk4s?file=src/app/app.component.ts
Upvotes: 1
Views: 6349
Reputation: 1043
You need to add @ViewChild('cd', { static: false }) private countdown: CountdownComponent;
Then add some buttons and bind the actions:
template:
<countdown #cd [config]="{leftTime: timeData}" (event)="handleEvent($event)">
</countdown>
<button (click)="pause()">pause</button>
<button (click)="start()">start</button>
functions:
pause(){
this.countdown.pause();
}
start(){
this.countdown.begin();
}
example: https://stackblitz.com/edit/ngx-countdown-setup-qo8kfq?file=src%2Fapp%2Fapp.component.ts
Edit:
add "notify" property to config:
[config]="{leftTime: timeData, notify: [ 2, 5 ]}"
then when the timer hit 5 & 2 notify event will be fired
handleEvent(event){
if(event.action === 'notify'){
console.log('Hi!');
}
}
example: https://stackblitz.com/edit/ngx-countdown-setup-qo8kfq?file=src%2Fapp%2Fapp.component.ts
Upvotes: 3
Reputation: 429
This may help you
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<countdown [config]="config" (event)="handleEvent($event)"></countdown>
<button (click)="start()">START</button>
<button *ngIf="showStop" (click)="stop()">STOP</button>
`
})
export class AppComponent {
timeData = "60"
config:any;
showStop:boolean;
constructor(){
}
public ngOnInit() {
this.config = {leftTime: this.timeData, demand:true};
}
start(){
this.config = {leftTime:this.timeData, demand:false};
this.showStop=true;
}
stop(){
this.config = {leftTime:this.timeData, demand:true};
this.showStop=false;
}
handleEvent(event){
console.log(event)
}
}
Upvotes: 1