ng-countdownjs
Version:
countdown component for angular
62 lines (52 loc) • 1.35 kB
text/typescript
import { Component, Input, OnInit, Output, EventEmitter } from '@angular/core';
import * as countdown from 'countdown';
export class CountdownComponent implements OnInit {
auto: boolean;
key: string;
time: number | Date;
units: number;
changed = new EventEmitter<countdown.Timespan>();
started = new EventEmitter<boolean>();
private timer: any;
ngOnInit() {
if (this.auto) {
this.start();
}
}
start(end?: number | Date) {
this.time = end || this.time;
if (this.key) {
let time = +localStorage.getItem(this.key);
if (time > new Date().getTime()) {
this.time = time;
} else {
time = this.time instanceof Date ? this.time.getTime() : this.time;
localStorage.setItem(this.key, time.toString());
}
}
this.timer = countdown(
ts => {
if (ts.value < 1000 && !ts.seconds) {
this.stop();
}
this.changed.next(ts);
},
this.time,
this.units,
);
this.started.next(true);
}
restart(end?: number | Date) {
this.stop();
this.start(end);
}
stop() {
clearInterval(this.timer);
this.started.next(false);
}
}