I'm trying to make a countdown timer for my Ionic2 app, the thing is that I was using this method from now countdown timer
but now I have to create the countdown like 30:00 min, what's the better way to do it? Time could change, and if I want to fire something when the countdown it's done I only have to be comparing the time
if it's 0, right?
Asked
Active
Viewed 4.7k times
11
3 Answers
42
You can 'listen' to the timer and trigger the action when the countdown is 0. And to display the timer, use a pipe.
HTML
{{counter | formatTime}}
TypeScript
countDown:Subscription;
counter = 1800;
tick = 1000;
ngOnInit() {
this.countDown = timer(0, this.tick)
.subscribe(() => --this.counter)
}
ngOnDestroy(){
this.countDown=null;
}
Pipe
//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);
}
If you wish to use a service:
Service
...
getCounter(tick) {
return timer(0, tick)
}
...
Component
countDown;
counter=1800 ;
tick=1000;
constructor(private myService: MyService) {
}
ngOnInit() {
this.countDown = this.myService.getCounter(this.tick).subscribe(() => this.counter--);
}
ngOnDestroy(){
this.countDown=null;
}
Pipe
...
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);
}

Vega
- 27,856
- 27
- 95
- 103
2
Try this for timer:
<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');
}
})
}
}

Shubham Azad
- 786
- 2
- 10
- 25

Chandru
- 10,864
- 6
- 38
- 53
-
I'll test this answer, to check what is more efficient, thanks @Chandru, by the way if instead of "timer" I have an api call that returns this : remainingTime:1244.529 with your option is possible to do that? – StuartDTO Sep 20 '17 at 10:46
-
Yeah it's possible using this code .. set the remainingTime:1244.529 instead of 'timer' then the timer start with 20.44 – Chandru Sep 20 '17 at 10:50
-
and if I have this on a service, it can update the view ? I mean the countdown is running but if I do an api call and returns a different remaingingTime I have to update it, it is possible? – StuartDTO Sep 20 '17 at 12:44
-
if you want to help me with that problem come on this [question](https://stackoverflow.com/questions/46338921/upload-a-view-from-service-every-x-seconds-ionic2) – StuartDTO Sep 21 '17 at 09:27
-
chandru with your answer I can have the observable stuff in the service, and from page get the value? I mean service have the observable and on page I have the "timer" (in your code)? – StuartDTO Sep 27 '17 at 12:38
-
-
After time up, timer and observer is still running how can I unsubscribe that observer ? – Rajat.r2 Oct 16 '20 at 15:34
0
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);
}

Sylvin Rodrigues
- 19
- 1
Countdown timer
{{[ERROR ->]countDown | async | formatTime}}