I'm using angular 5 and I have a mat-progress-bar. I also have a timer with 2 * 60 value that means 2 minutes. I want to decrease the value of progress-bar each second and after 2 minutes the value of bar become 0! how can I do it?
You can use the following code as an example.
The component class will look like this:
export class AppComponent {
progressbarValue = 100;
curSec: number = 0;
startTimer(seconds: number) {
const time = seconds;
const timer$ = interval(1000);
const sub = timer$.subscribe((sec) => {
this.progressbarValue = 100 - sec * 100 / seconds;
this.curSec = sec;
if (this.curSec === seconds) {
sub.unsubscribe();
}
});
}
}
In the template you've the progress bar that uses the value of progressbarValue
:
<mat-progress-bar mode="determinate" [value]="progressbarValue"></mat-progress-bar>
And a button to start the startTimer
method:
<button mat-raised-button (click)="startTimer(60)">Start timer</button>
You can find a running code example here:
https://stackblitz.com/edit/angular-material-progress-bar-decrease
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With