Angular 2反向倒数计时器

时间:2017-12-02 09:59:35

标签: javascript angular typescript

我正在为Angular项目寻找倒数倒数计时器。这样的事情:https://harshen.github.io/jquery-countdownTimer/

我尝试了所有的observable和rxjs计时器,但似乎没有什么能满足我的项目需求。有人可以帮助我使用反向倒数计时器或修改上述链接代码以用于Angular项目吗?

1 个答案:

答案 0 :(得分:0)

export class CountDown {

    // Hardcoded date
    private endDate: Date = new Date(2018, 9, 22);

    private diff: number;
    private countDownResult: number;
    private days: number;
    private hours: number;
    private minutes: number;
    private seconds: number;

    constructor() {

        Observable.interval(1000).map((x) => {
                this.diff = Math.floor((this.endDate.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;
    }