2017-09-20 43 views
3

我想爲我的Ionic2應用程序做一個倒計時器,事情是我從現在使用這種方法countdown timer,但現在我必須創建像30:00分鐘倒計時,什麼是更好如何做到這一點?時間可能會改變,如果我想在倒計時完成時發射一些東西,我只需要比較time,如果它是0的話,對吧?定時器倒計時角2

回答

8

您可以'聽'定時器,並在倒計時爲0時觸發動作。要顯示定時器,請使用管道。

HTML

<h2>{{countDown | async | formatTime}}</h2> 

<button [disabled]="counter == 0">OK</button> 

打字稿

countDown; 
    counter = 30*60; 
    tick = 1000; 
    ngOnInit() { 
    this.countDown = Observable.timer(0, this.tick) 
     .take(this.counter) 
     .map(() => --this.counter) 
    } 

//for MM:SS format 

@Pipe({ 
    name: 'formatTime' 
}) 
export class FormatTimePipe implements PipeTransform { 

    transform(value: number): string { 
     const minutes: number = Math.floor(value/60); 
     return ('00'+ minutes).slice(-2) + ':' + ('00'+Math.floor(value-minutes * 60)).slice(-2); 
    } 

} 

DEMO

//for HH:MM:SS format 

transform(value: number): string { 
    const hours: number = Math.floor(value/3600); 
    const minutes: number = Math.floor((value % 3600)/60); 
    return ('00' + hours).slice(-2) + ':' + ('00' + minutes).slice(-2) + ':' + ('00' + Math.floor(value - minutes * 60)).slice(-2); 
} 

DEMO


如果您希望使用的服務:

服務

... 
getCounter() { 
    return Observable.timer(0, this.tick) 
     .take(this.counter) 
     .map(() => --this.counter) 
    } 
... 

組件

@Component({ 
    ... 
    providers: [MyService] 
}) 
... 
constructor(private myService: MyService) {} 
ngOnInit(){ 
    this.countDown = this.myService.getCounter().do(() => --this.counter); 
    //or 
    //this.countDown = myService.getCounter(); 
} 
... 

... 
    transform(value: number): string { 
    //MM:SS format 
    const minutes: number = Math.floor(value/60); 
    return ('00' + minutes).slice(-2) + ':' + ('00' + Math.floor(value - minutes * 60)).slice(-2); 

    // for HH:MM:SS 
    //const hours: number = Math.floor(value/3600); 
    //const minutes: number = Math.floor((value % 3600)/60); 
    //return ('00' + hours).slice(-2) + ':' + ('00' + minutes).slice(-2) + ':' + ('00' + Math.floor(value - minutes * 60)).slice(-2); 

    } 

DEMO

+0

這就是我鏈接的答案的相同代碼,對吧?如果我想用這個30:00,我不能做到這一點 – Stuart2041

+0

另外我可以把它放在一個服務,然後將其同步到我的頁面? – Stuart2041

+0

如果你把它放在服務中,訂閱這個 – Vega

0

試試這個定時器:

<h5><span id="time" class="text-primary">00:00</span></h5> 

component.ts

import { Component, OnInit, ElementRef, AfterViewInit } from '@angular/core'; 
import { Observable } from 'rxjs/Rx'; 

export class AppComponent implements OnInit { 

    constructor(private elementRef: ElementRef) { } 

    ngOnInit() { 
     var callDuration = this.elementRef.nativeElement.querySelector('#time'); 
     this.startTimer(callDuration); 
    } 

    startTimer(display) { 
     var timer = 1800; 
     var minutes; 
     var seconds; 

     Observable.interval(1000).subscribe(x => { 
      minutes = Math.floor(timer/60); 
      seconds = Math.floor(timer % 60); 

      minutes = minutes < 10 ? "0" + minutes : minutes; 
      seconds = seconds < 10 ? "0" + seconds : seconds; 

      display.textContent = minutes + ":" + seconds; 

      --timer; 
      if (--timer < 0) { 
       console.log('timeup'); 
      } 
     }) 
    } 
} 
+0

我會測試這個答案,檢查什麼是更有效率,謝謝@Chandru,順便說一句,如果不是「計時器」,我有一個API調用,返回這個:remainingTime:1244.529與您選項可以做到這一點? – Stuart2041

+0

是的,這是可能的使用此代碼..設置剩餘時間:1244.529而不是'定時器',那麼計時器開始20.44 – Chandru

+0

,如果我有這個服務,它可以更新視圖?我的意思是倒計時正在運行,但如果我做了一個API調用,並返回一個不同的remaingingTime,我不得不更新它,這有可能嗎? – Stuart2041