【发布时间】:2021-10-22 07:37:09
【问题描述】:
这是我的打字稿文件
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();
});
}
}
我在我的逻辑中遇到问题当我选择它在我的变量中显示的时间但倒计时是没有开始,倒计时没有反应请任何人都可以帮助我如何做到这一点
这是我要显示倒计时的 HTML 文件
<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>
【问题讨论】:
-
请创建一个包含此问题的 stackblitz 项目。
-
你能解决这个问题吗
-
请创建一个 stackblitz 角度项目并在那里创建相同的代码并将链接与问题一起放置,以便我可以帮助您
-
什么时候我会创建 stackblitz 项目,然后我会发给你链接,请帮帮我,我在三天后就陷入了这个问题,我不知道如何使用时间选择器创建倒数计时器
-
这是 satackblitz 链接stackblitz.com/edit/…
标签: angular timer subscription angular-bootstrap