Time CountDown in angular 2

前端 未结 6 1414
花落未央
花落未央 2020-12-13 01:41

I want to have a date countdown like this:

http://codepen.io/garethdweaver/pen/eNpWBb

but in angular 2, I found this plunkr that adds 1 to a number each 500

6条回答
  •  执笔经年
    2020-12-13 02:24

    Here's a solution i came up with recently on my project. It checks between a gate opening time of an event and the main event time of the same event. This uses Momentjs.

    I have code in the onChanges function in case an event time changes in my firebase which is very unlikely, but i like the fact it can pick up on it and update on the fly.

    Component:

    import { Component, OnInit, Input, OnChanges } from '@angular/core';
    import { Observable } from "rxjs/Rx"
    import { RoundService } from '../../round.service'
    import * as moment from 'moment-timezone';
    
    @Component({
      selector: 'race-countdown',
      templateUrl: './race-countdown.component.html',
      styleUrls: ['./race-countdown.component.scss']
    })
    export class RaceCountdownComponent implements OnChanges,OnInit{
    
        @Input() activeRound;
    
        public time:number;
        public timeToGates:number;
        public timeToMains:number;  
        public countDown:Observable;
        public currentUnix = moment().unix();
    
        constructor(private rs:RoundService) { }
    
        ngOnChanges() {
    
        this.timeToGates = this.activeRound.gateOpenUnix - this.currentUnix;
        this.timeToMains = this.activeRound.mainEventUnix - this.currentUnix;
    
        if(this.timeToGates < 0)
            this.time = this.timeToMains
        else 
            this.time = this.timeToGates
    
      }
    
      ngOnInit() {
    
        this.countDown = Observable.interval(1000)
                                              .map(res=>{ return this.time = this.time-1 })
                                              .map(res=> {
    
                                                let timeLeft = moment.duration(res, 'seconds');
                                                let string:string = '';
    
                                                // Days.
                                                if(timeLeft.days() > 0) 
                                                    string += timeLeft.days() + ' Days'
    
                                                // Hours.
                                                if(timeLeft.hours() > 0) 
                                                    string += ' ' + timeLeft.hours() + ' Hours'
    
                                                // Minutes.
                                                if(timeLeft.minutes() > 0)
                                                    string += ' ' + timeLeft.minutes() + ' Mins'
    
                                                // Seconds.
                                                string += ' ' + timeLeft.seconds(); 
    
                                                return string;
    
                                              })
    
      }
    
    }
    

    HTML:

    {{countDown | async}}
    

    Produces: '2 Days 6 Hours 59 Mins 42' etc

提交回复
热议问题