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 bee 0! how can I do it?
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 bee 0! how can I do it?
Share Improve this question edited Oct 20, 2018 at 10:50 Philipp Kief 8,6135 gold badges35 silver badges43 bronze badges asked Oct 20, 2018 at 7:41 fariba.jfariba.j 1,8657 gold badges26 silver badges42 bronze badges1 Answer
Reset to default 14You can use the following code as an example.
The ponent 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./edit/angular-material-progress-bar-decrease