This is my Typescript file
import { Time } from '@angular/common';
import { Component, OnInit, Pipe, PipeTransform } from '@angular/core';
import { interval, Subscription, timer } from 'rxjs';
import {NgbTimeStruct} from '@ng-bootstrap/ng-bootstrap';
@Component({
selector: 'app-timer-app',
templateUrl: './timer-app.component.html',
styleUrls: ['./timer-app.component.css']
})
export class TimerAppComponent implements OnInit {
time : NgbTimeStruct= {hour: 13, minute: 30 , second: 25};
seconds = true;
constructor() { }
ngOnInit() {
this.startInterval();
console.log(this.time)
}
ngOnDestroy(){
this.subscription.unsubscribe();
}
stop(){
this.countDown.unsubscribe();
}
start(){
this.countDown = timer(0, this.tick).subscribe(() => --this.counter);
}
private timeinterval = interval;
private subscription!: Subscription;
public dateNow = new Date();
public dDay = this.model.getTime();
milliSecondsInASecond = 1000;
hoursInADay = 24;
minutesInAnHour = 60;
SecondsInAMinute = 60;
public timeDifference: any;
public secondsToDday: any;
public minutesToDday: any;
public hoursToDday: any;
public daysToDday: any;
private getTimeDifference() {
this.dDay = this.time
this.timeDifference = this.dDay - new Date().getTime();
this.allocateTimeUnits(this.timeDifference);
}
private allocateTimeUnits(timeDifference: any) {
this.secondsToDday = Math.floor(
(timeDifference / this.milliSecondsInASecond) % this.SecondsInAMinute
);
this.minutesToDday = Math.floor(
(timeDifference / (this.milliSecondsInASecond * this.minutesInAnHour)) %
this.SecondsInAMinute
);
this.hoursToDday = Math.floor(
(timeDifference /
(this.milliSecondsInASecond *
this.minutesInAnHour *
this.SecondsInAMinute)) %
this.hoursInADay
);
this.daysToDday = Math.floor(
timeDifference /
(this.milliSecondsInASecond *
this.minutesInAnHour *
this.SecondsInAMinute *
this.hoursInADay)
);}
stopInterval() {
this.subscription.unsubscribe();
}
startInterval() {
this.subscription = this.timeinterval(1000).subscribe(x => {
// console.log('get TD', this.timeDifference);
this.getTimeDifference();
});
}
}
I'm facing the problem in my logic I don't know how to perform the logic for starting the count down after selecting the time from the input when I select the time it is displaying in my variable but the count down is not started, and the count down gives no response please anyone can help me how I can do this
This is my HTML file where I want to show the count down
<div class="container">
<h1 class="text-success">Date Picker App</h1>
<ngb-timepicker [(ngModel)]="time" [seconds]="seconds" ></ngb-timepicker>
<hr>
<pre> Seleted TIme Is : <strong>{{newTime}}</strong></pre>
</div>
<!--Count Down Timer DIsplay -->
<div class="timer" *ngIf="model">
<h2>Time Left</h2>
<span id="hours"> {{hoursToDday}} </span>Hrs
<span id="minutes"> {{minutesToDday}} </span>Min
<span id="seconds"> {{secondsToDday}} </span>S <br>
<div class="mt-3">
<button (click)="stopInterval()" class="btn btn-danger btn-
sm">Stop</button>
<button (click)="startInterval()" class="btn btn-info btn-sm
"id="resetbtn">Restart</button>
</div>