我想爲我的Ionic2應用程序做一個倒計時器,事情是我從現在使用這種方法countdown timer
,但現在我必須創建像30:00分鐘倒計時,什麼是更好如何做到這一點?時間可能會改變,如果我想在倒計時完成時發射一些東西,我只需要比較time
,如果它是0的話,對吧?定時器倒計時角2
定時器倒計時角2
回答
您可以'聽'定時器,並在倒計時爲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);
}
}
//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);
}
如果您希望使用的服務:
服務
...
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);
}
試試這個定時器:
<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');
}
})
}
}
我會測試這個答案,檢查什麼是更有效率,謝謝@Chandru,順便說一句,如果不是「計時器」,我有一個API調用,返回這個:remainingTime:1244.529與您選項可以做到這一點? – Stuart2041
是的,這是可能的使用此代碼..設置剩餘時間:1244.529而不是'定時器',那麼計時器開始20.44 – Chandru
,如果我有這個服務,它可以更新視圖?我的意思是倒計時正在運行,但如果我做了一個API調用,並返回一個不同的remaingingTime,我不得不更新它,這有可能嗎? – Stuart2041
- 1. C#timer_Tick()倒計時2步倒計時
- 2. 定時器倒計時
- 3. WPF定時器倒計時
- 4. 角度2多個倒計時管道
- 5. JQuery倒計時器不倒計時
- 6. 倒計時 - iPhone倒數計時器
- 7. 修復倒計時倒數計時器
- 8. 從mysql的倒計時器定時器
- 9. jquery定時器或倒數計時器
- 10. ActionScript 3 - 定時器/倒數計時器
- 11. 定時器,倒計時,時間
- 12. 定時器倒計時時間格式
- 13. 倒計時器
- 14. 倒計時計時器android
- 15. 倒計時計時器
- 16. JavaScript定時器倒計時退出
- 17. iphone定時器倒計時在後臺
- 18. 倒計時定時器線程
- 19. 定時器倒計時sql,asp.net c#,javascript
- 20. jquery定時器倒計時和countup
- 21. 創建定時器倒計時VB.Net?
- 22. 定時器錯誤倒計時
- 23. 定時器倒計時SQL,asp.net C#
- 24. JQuery倒計時定時器延遲
- 25. 重置我的定時器倒計時
- 26. 定時器倒計時MVC 3.0
- 27. 定時器顯示到倒計時
- 28. jquery倒計時定時器NaN
- 29. 的Java定時器倒計時的JTable
- 30. Android中的定時器倒計時
這就是我鏈接的答案的相同代碼,對吧?如果我想用這個30:00,我不能做到這一點 – Stuart2041
另外我可以把它放在一個服務,然後將其同步到我的頁面? – Stuart2041
如果你把它放在服務中,訂閱這個 – Vega