Angular2,Ionic2:如何创建一个小时,分钟,秒和毫秒的倒数计时器?

时间:2017-06-14 12:43:44

标签: typescript timer ionic2 angular2-services countdowntimer

我想创建一个倒计时器,它有4个小时小时:分钟:秒:毫秒。 在发布这个问题之前,我已经浏览了以下链接。我不知道为什么我们应该有一个硬编码的日期,然后从中减去一些东西。我不能在这里得到Observable的概念。

Time CountDown in angular 2

Angular 2 - Countdown timer

有人可以帮帮我吗?

这是我从https://stackoverflow.com/a/40784539/4579897

写的
private eventDate: Date = new Date(2018, 1, 15);
private diff: number;
private countDownResult: number;
private days: number;
private hours: number;
private minutes: number;
private seconds: number;

constructor(public navCtrl: NavController, public navParams: NavParams,elm: ElementRef) {
      Observable.interval(1000).map((x) => {
            this.diff = Math.floor((this.eventDate.getTime() - new Date().getTime()) / 1000);
        }).subscribe((x) => {           
            this.days = this.getDays(this.diff);
            this.hours = this.getHours(this.diff);
            this.minutes = this.getMinutes(this.diff);
            this.seconds = this.getSeconds(this.diff);
        });
}

getDays(t){
    var days;
    days = Math.floor(t / 86400);

    return days;
}

getHours(t){
    var days, hours;
    days = Math.floor(t / 86400);
    t -= days * 86400;
    hours = Math.floor(t / 3600) % 24;

    return hours;
}

getMinutes(t){
    var days, hours, minutes;
    days = Math.floor(t / 86400);
    t -= days * 86400;
    hours = Math.floor(t / 3600) % 24;
    t -= hours * 3600;
    minutes = Math.floor(t / 60) % 60;

    return minutes;
}

getSeconds(t){
    var days, hours, minutes, seconds;
    days = Math.floor(t / 86400);
    t -= days * 86400;
    hours = Math.floor(t / 3600) % 24;
    t -= hours * 3600;
    minutes = Math.floor(t / 60) % 60;
    t -= minutes * 60;
    seconds = t % 60;

    return seconds;
}

但是这里有“天”,我不想要它。我想要几毫秒而且如果有人可以解释我做了什么

 Observable.interval(1000).map((x) => {
                this.diff = Math.floor((this.eventDate.getTime() - new Date().getTime()) / 1000);
            })
是什么意思?我想知道html页面中的代码应该是什么。 在此先感谢!!

0 个答案:

没有答案