I am using angular. I have created custom stopwatch. I am managing the start/end button for each ngFor item. but not able to manage unique timer for each ngFor item.
in the above image, I have different start/end button each item. when I click on the start timer button of item A, the timer starts of both items.
public seconds = 0; minutes = 0; hours = 0; t; h1 = "00:00:00";
startTask (item) {
if(item.start) {
item.end = true;
item.start= false;
}
this.timer()
}
EndTask (item) {
if(item.end) {
item.end = false;
item.start= true;
}
clearTimeout(this.t);
}
add() {
this.seconds++;
if (this.seconds >= 60) {
this.seconds = 0;
this.minutes++;
if (this.minutes >= 60) {
this.minutes = 0;
this.hours++;
}
}
this.h1 = (this.hours ? (this.hours > 9 ? this.hours : "0" + this.hours) : "00") + ":" + (this.minutes ? (this.minutes > 9 ? this.minutes : "0" + this.minutes) : "00") + ":" + (this.seconds > 9 ? this.seconds : "0" + this.seconds);
this.timer();
}
timer() {
this.t = setTimeout(() => {
this.add()
}, 1000);
}
reset() {
this.h1 = "00:00:00";
this.seconds = 0;
this.minutes = 0;
this.hours = 0;
}
<div class="row no-gutters">
<div class="card width hr" *ngFor="let item of allUserTaskArr">
<div class="card-header">
<span>{{item.due | date}}</span>
<span class="float-right font-weight-bold">{{h1}}</span>
</div>
<div class="card-body pad-125">
<div class="row no-gutters">
<div class="col-md-12">
{{item.name}}
<div class="float-right">
<button class="btn btn-info mar-l-r-0-5" *ngIf="item.start" (click)="startTask(item)">Start</button>
<button class="btn btn-danger mar-l-r-0-5" *ngIf="item.end" (click)="EndTask(item)">End</button>
</div>
</div>
</div>
</div>
</div>
</div>
I want that when I click on the start button of item A. the timer should start for item A. when the timer for item A is started & I click on item B's start button the previous timer should pause & new timer should start. actually, I want to maintain the time.