angular-countdown-timer
Version:
Countdown Timer for Angular 7+
127 lines (123 loc) • 3.69 kB
JavaScript
import { Component, ElementRef, EventEmitter, Input, NgModule, Output } from '@angular/core';
import { CommonModule } from '@angular/common';
var CountdownTimer = (function () {
/**
* @param {?} el
*/
function CountdownTimer(el) {
this.el = el;
this.zeroTrigger = new EventEmitter(true);
}
/**
* @return {?}
*/
CountdownTimer.prototype.ngOnInit = function () {
var _this = this;
this.timer = setInterval(function () {
if (_this.start) {
_this.displayTime = _this.getTimeDiff(_this.start, true);
}
else {
_this.displayTime = _this.getTimeDiff(_this.end);
}
}, 1000);
};
/**
* @return {?}
*/
CountdownTimer.prototype.ngOnDestroy = function () {
this.stopTimer();
};
/**
* @param {?} datetime
* @param {?=} useAsTimer
* @return {?}
*/
CountdownTimer.prototype.getTimeDiff = function (datetime, useAsTimer) {
if (useAsTimer === void 0) { useAsTimer = false; }
datetime = new Date(datetime).getTime();
var /** @type {?} */ now = new Date().getTime();
if (isNaN(datetime)) {
return "";
}
var /** @type {?} */ milisec_diff = datetime - now;
if (useAsTimer) {
milisec_diff = now - datetime;
}
// Zero Time Trigger
if (milisec_diff <= 0 ) {
this.zeroTrigger.emit("reached zero");
return "00:00:00:00";
}
var /** @type {?} */ days = Math.floor(milisec_diff / 1000 / 60 / (60 * 24));
var /** @type {?} */ date_diff = new Date(milisec_diff);
var /** @type {?} */ day_string = (days) ? this.twoDigit(days) + ":" : "";
// Date() takes a UTC timestamp – getHours() gets hours in local time not in UTC. therefore we have to use getUTCHours()
return day_string + this.twoDigit(date_diff.getUTCHours()) +
":" + this.twoDigit(date_diff.getUTCMinutes()) + ":"
+ this.twoDigit(date_diff.getUTCSeconds());
};
/**
* @param {?} number
* @return {?}
*/
CountdownTimer.prototype.twoDigit = function (number) {
return number > 9 ? "" + number : "0" + number;
};
/**
* @return {?}
*/
CountdownTimer.prototype.stopTimer = function () {
clearInterval(this.timer);
this.timer = undefined;
};
return CountdownTimer;
}());
CountdownTimer.decorators = [
{ type: Component, args: [{
selector: 'countdown-timer',
template: "{{ displayTime }}"
},] },
];
/**
* @nocollapse
*/
CountdownTimer.ctorParameters = function () { return [
{ type: ElementRef, },
]; };
CountdownTimer.propDecorators = {
'start': [{ type: Input },],
'end': [{ type: Input },],
'zeroTrigger': [{ type: Output },],
};
var CountdownTimerModule = (function () {
function CountdownTimerModule() {
}
/**
* @return {?}
*/
CountdownTimerModule.forRoot = function () {
return {
ngModule: CountdownTimerModule
};
};
return CountdownTimerModule;
}());
CountdownTimerModule.decorators = [
{ type: NgModule, args: [{
imports: [
CommonModule
],
declarations: [
CountdownTimer,
],
exports: [
CountdownTimer
]
},] },
];
/**
* @nocollapse
*/
CountdownTimerModule.ctorParameters = function () { return []; };
export { CountdownTimerModule, CountdownTimer };