最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to change a mat-progress-bar value in an interval? - Stack Overflow

programmeradmin4浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 14

You 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

发布评论

评论列表(0)

  1. 暂无评论