计时器倒计时 Angular 2
Timer countdown Angular 2
我正在尝试为我的 Ionic2 应用程序制作一个倒数计时器,问题是我从现在开始使用这种方法 但现在我必须创建像 30:00 分钟这样的倒计时, 什么是更好的方法呢?时间可能会改变,如果我想在倒计时结束时开火,我只需要比较 time
如果它是 0,对吗?
你可以'listen'到计时器并在倒计时为0时触发动作。要显示计时器,请使用管道。
HTML
{{counter | formatTime}}
TypeScript
countDown:Subscription;
counter = 1800;
tick = 1000;
ngOnInit() {
this.countDown = timer(0, this.tick)
.subscribe(() => --this.counter)
}
ngOnDestroy(){
this.countDown=null;
}
管道
//for MM:SS format
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(tick) {
return timer(0, tick)
}
...
组件
countDown;
counter=1800 ;
tick=1000;
constructor(private myService: MyService) {
}
ngOnInit() {
this.countDown = this.myService.getCounter(this.tick).subscribe(() => this.counter--);
}
ngOnDestroy(){
this.countDown=null;
}
管道
...
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('timer is ended');
}
})
}
}
maxtime: any=30
StartTimer(){
this.timer = setTimeout(x =>
{
if(this.maxTime <= 0) { }
this.maxTime -= 1;
if(this.maxTime>0){
this.hidevalue = false;
this.StartTimer();
}
else{
this.hidevalue = true;
}
}, 1000);
}
我正在尝试为我的 Ionic2 应用程序制作一个倒数计时器,问题是我从现在开始使用这种方法 time
如果它是 0,对吗?
你可以'listen'到计时器并在倒计时为0时触发动作。要显示计时器,请使用管道。
HTML
{{counter | formatTime}}
TypeScript
countDown:Subscription;
counter = 1800;
tick = 1000;
ngOnInit() {
this.countDown = timer(0, this.tick)
.subscribe(() => --this.counter)
}
ngOnDestroy(){
this.countDown=null;
}
管道
//for MM:SS format
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(tick) {
return timer(0, tick)
}
...
组件
countDown;
counter=1800 ;
tick=1000;
constructor(private myService: MyService) {
}
ngOnInit() {
this.countDown = this.myService.getCounter(this.tick).subscribe(() => this.counter--);
}
ngOnDestroy(){
this.countDown=null;
}
管道
...
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('timer is ended');
}
})
}
}
maxtime: any=30
StartTimer(){
this.timer = setTimeout(x =>
{
if(this.maxTime <= 0) { }
this.maxTime -= 1;
if(this.maxTime>0){
this.hidevalue = false;
this.StartTimer();
}
else{
this.hidevalue = true;
}
}, 1000);
}