2017-12-02 7 views
1

각도 프로젝트의 역 카운트 다운 타이머를 찾고 있습니다. 뭔가 이런 : https://harshen.github.io/jquery-countdownTimer/각도 2 역 카운트 다운 타이머

나는 observable 및 rxjs 타이머를 시도했지만 아무것도 내 프로젝트의 필요성을 만족하는 것 같습니다. 다른 사람이 역 카운트 다운 타이머를 사용하거나 위의 링크 코드를 수정하여 각도 프로젝트에 사용할 수 있습니까?

+0

RxJS에는 기본적으로 이와 같은 내용이 없습니다 ... npm 라이브러리를 찾으려고하거나 직접 작성하면 상당히 간단합니다. –

+0

@ AnteJablanAdamović, 당신은 아래 FAIRLY 간단하고 쉬운 코드를 확인할 수 있습니다. –

답변

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; 
    }