Commit 9963cb28 authored by wildfirecode13's avatar wildfirecode13

1

parent 4d20b0ad
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title> <title>Document</title>
</head> </head>
<body> <body>
<div id="root"></div>
<script src="teddi.umd.js"></script> <script src="teddi.umd.js"></script>
<script> <script>
const timer = new teddi.Countdown( 86400 * 2 +3600*1 + 60*1 +1, 'ddhhmmss',1000);
timer.on('update', (data) => {
const {dd,hh,mm,ss} = data;
document.getElementById('root').innerHTML = `${dd}:${hh}:${mm}:${ss}`;
// document.getElementById('root').innerHTML = `${hh}:${mm}:${ss}`;
// document.getElementById('root').innerHTML = `${mm}:${ss}`;
}, this);
timer.start();
</script> </script>
</body> </body>
</html> </html>
\ No newline at end of file
...@@ -393,46 +393,66 @@ ...@@ -393,46 +393,66 @@
} }
var Countdown = /** @class */ (function (_super) { var Countdown = /** @class */ (function (_super) {
tslib_1.__extends(Countdown, _super); tslib_1.__extends(Countdown, _super);
function Countdown(text, seconds, format, delay) { function Countdown(repeatCount, format, delay) {
var _this = _super.call(this) || this; var _this = _super.call(this) || this;
_this._running = false; _this._running = false;
_this._text = text; _this._repeatCount = repeatCount;
_this._seconds = seconds;
_this._format = format; _this._format = format;
_this._counter = 0; _this._counter = 0;
_this._delay = delay || 1000; _this._delay = delay || 1000;
return _this; return _this;
} }
Countdown.prototype.getCountdown = function () { Countdown.prototype.getCountdown = function () {
if (this._format == "ddhhmmss")
return this.getDDHHMMSS();
if (this._format == "hhmmss") if (this._format == "hhmmss")
return this.getHHMMSS(); return this.getHHMMSS();
if (this._format == "mmss") if (this._format == "mmss")
return this.getMMSS(); return this.getMMSS();
}; };
Countdown.prototype.getDDHHMMSS = function () {
var left = this._repeatCount - this._counter;
var days = Math.floor(left / 86400);
var dayMod = left % 86400;
var hours = Math.floor(dayMod / 3600);
var hoursMod = dayMod % 3600;
var minutes = Math.floor(hoursMod / 60);
var seconds = hoursMod % 60;
return {
dd: getTimeNum(days),
hh: getTimeNum(hours),
mm: getTimeNum(minutes),
ss: getTimeNum(seconds)
};
};
Countdown.prototype.getHHMMSS = function () { Countdown.prototype.getHHMMSS = function () {
var left = this._seconds - this._counter; var left = this._repeatCount - this._counter;
var hours = Math.floor(left / 3600); var hours = Math.floor(left / 3600);
var minutes = Math.floor((left % 3600) / 60); var minutes = Math.floor((left % 3600) / 60);
var seconds = left - hours * 3600 - minutes * 60; var seconds = left - hours * 3600 - minutes * 60;
return getTimeNum(hours) + ":" + getTimeNum(minutes) + ":" + getTimeNum(seconds); return {
hh: getTimeNum(hours),
mm: getTimeNum(minutes),
ss: getTimeNum(seconds)
};
}; };
Countdown.prototype.getMMSS = function () { Countdown.prototype.getMMSS = function () {
var left = this._seconds - this._counter; var left = this._repeatCount - this._counter;
var minutes = Math.floor(left / 60); var minutes = Math.floor(left / 60);
var seconds = left % 60; var seconds = left % 60;
return getTimeNum(minutes) + ":" + getTimeNum(seconds); return {
}; mm: getTimeNum(minutes),
Countdown.prototype.getText = function () { ss: getTimeNum(seconds)
return this._text.replace("{0}", this.getCountdown()); };
}; };
Countdown.prototype.start = function () { Countdown.prototype.start = function () {
var _this = this; var _this = this;
this._running = true; this._running = true;
this.emit("update", this.getText()); this.emit("update", this.getCountdown());
this._timer = setInterval(function () { this._timer = setInterval(function () {
_this._counter++; _this._counter++;
_this.emit("update", _this.getText()); _this.emit("update", _this.getCountdown());
if (_this._counter == _this._seconds) { if (_this._counter == _this._repeatCount) {
_this.stop(); _this.stop();
} }
}, this._delay); }, this._delay);
...@@ -445,7 +465,7 @@ ...@@ -445,7 +465,7 @@
Countdown.prototype.reset = function ($seconds) { Countdown.prototype.reset = function ($seconds) {
clearInterval(this._timer); clearInterval(this._timer);
this._counter = 0; this._counter = 0;
this._seconds = $seconds; this._repeatCount = $seconds;
}; };
Object.defineProperty(Countdown.prototype, "running", { Object.defineProperty(Countdown.prototype, "running", {
// set seconds(val: number) { // set seconds(val: number) {
......
import { EventEmitter } from "../event"; import { EventEmitter } from "../event";
export declare class Countdown extends EventEmitter<any> { export declare class Countdown extends EventEmitter<any> {
private _timer; private _timer;
private _text; private _repeatCount;
private _seconds;
private _format; private _format;
private _counter; private _counter;
private _delay; private _delay;
private _running; private _running;
constructor(text: any, seconds: any, format: any, delay: any); constructor(repeatCount: any, format: any, delay: any);
private getCountdown; private getCountdown;
private getDDHHMMSS;
private getHHMMSS; private getHHMMSS;
private getMMSS; private getMMSS;
private getText;
start(): void; start(): void;
private stop; private stop;
reset($seconds: number): void; reset($seconds: number): void;
......
...@@ -20,46 +20,66 @@ function getTimeNum(n) { ...@@ -20,46 +20,66 @@ function getTimeNum(n) {
} }
var Countdown = /** @class */ (function (_super) { var Countdown = /** @class */ (function (_super) {
__extends(Countdown, _super); __extends(Countdown, _super);
function Countdown(text, seconds, format, delay) { function Countdown(repeatCount, format, delay) {
var _this = _super.call(this) || this; var _this = _super.call(this) || this;
_this._running = false; _this._running = false;
_this._text = text; _this._repeatCount = repeatCount;
_this._seconds = seconds;
_this._format = format; _this._format = format;
_this._counter = 0; _this._counter = 0;
_this._delay = delay || 1000; _this._delay = delay || 1000;
return _this; return _this;
} }
Countdown.prototype.getCountdown = function () { Countdown.prototype.getCountdown = function () {
if (this._format == "ddhhmmss")
return this.getDDHHMMSS();
if (this._format == "hhmmss") if (this._format == "hhmmss")
return this.getHHMMSS(); return this.getHHMMSS();
if (this._format == "mmss") if (this._format == "mmss")
return this.getMMSS(); return this.getMMSS();
}; };
Countdown.prototype.getDDHHMMSS = function () {
var left = this._repeatCount - this._counter;
var days = Math.floor(left / 86400);
var dayMod = left % 86400;
var hours = Math.floor(dayMod / 3600);
var hoursMod = dayMod % 3600;
var minutes = Math.floor(hoursMod / 60);
var seconds = hoursMod % 60;
return {
dd: getTimeNum(days),
hh: getTimeNum(hours),
mm: getTimeNum(minutes),
ss: getTimeNum(seconds)
};
};
Countdown.prototype.getHHMMSS = function () { Countdown.prototype.getHHMMSS = function () {
var left = this._seconds - this._counter; var left = this._repeatCount - this._counter;
var hours = Math.floor(left / 3600); var hours = Math.floor(left / 3600);
var minutes = Math.floor((left % 3600) / 60); var minutes = Math.floor((left % 3600) / 60);
var seconds = left - hours * 3600 - minutes * 60; var seconds = left - hours * 3600 - minutes * 60;
return getTimeNum(hours) + ":" + getTimeNum(minutes) + ":" + getTimeNum(seconds); return {
hh: getTimeNum(hours),
mm: getTimeNum(minutes),
ss: getTimeNum(seconds)
};
}; };
Countdown.prototype.getMMSS = function () { Countdown.prototype.getMMSS = function () {
var left = this._seconds - this._counter; var left = this._repeatCount - this._counter;
var minutes = Math.floor(left / 60); var minutes = Math.floor(left / 60);
var seconds = left % 60; var seconds = left % 60;
return getTimeNum(minutes) + ":" + getTimeNum(seconds); return {
}; mm: getTimeNum(minutes),
Countdown.prototype.getText = function () { ss: getTimeNum(seconds)
return this._text.replace("{0}", this.getCountdown()); };
}; };
Countdown.prototype.start = function () { Countdown.prototype.start = function () {
var _this = this; var _this = this;
this._running = true; this._running = true;
this.emit("update", this.getText()); this.emit("update", this.getCountdown());
this._timer = setInterval(function () { this._timer = setInterval(function () {
_this._counter++; _this._counter++;
_this.emit("update", _this.getText()); _this.emit("update", _this.getCountdown());
if (_this._counter == _this._seconds) { if (_this._counter == _this._repeatCount) {
_this.stop(); _this.stop();
} }
}, this._delay); }, this._delay);
...@@ -72,7 +92,7 @@ var Countdown = /** @class */ (function (_super) { ...@@ -72,7 +92,7 @@ var Countdown = /** @class */ (function (_super) {
Countdown.prototype.reset = function ($seconds) { Countdown.prototype.reset = function ($seconds) {
clearInterval(this._timer); clearInterval(this._timer);
this._counter = 0; this._counter = 0;
this._seconds = $seconds; this._repeatCount = $seconds;
}; };
Object.defineProperty(Countdown.prototype, "running", { Object.defineProperty(Countdown.prototype, "running", {
// set seconds(val: number) { // set seconds(val: number) {
......
...@@ -6,52 +6,70 @@ function getTimeNum(n: any) { ...@@ -6,52 +6,70 @@ function getTimeNum(n: any) {
export class Countdown extends EventEmitter<any> { export class Countdown extends EventEmitter<any> {
private _timer: any; private _timer: any;
private _text: any; private _repeatCount: any;
private _seconds: any;
private _format: any; private _format: any;
private _counter: any; private _counter: any;
private _delay: any; private _delay: any;
private _running = false; private _running = false;
constructor(text: any, seconds: any, format: any, delay: any) { constructor(repeatCount: any, format: any, delay: any) {
super(); super();
this._text = text; this._repeatCount = repeatCount;
this._seconds = seconds;
this._format = format; this._format = format;
this._counter = 0; this._counter = 0;
this._delay = delay || 1000; this._delay = delay || 1000;
} }
private getCountdown() { private getCountdown() {
if (this._format == "ddhhmmss") return this.getDDHHMMSS();
if (this._format == "hhmmss") return this.getHHMMSS(); if (this._format == "hhmmss") return this.getHHMMSS();
if (this._format == "mmss") return this.getMMSS(); if (this._format == "mmss") return this.getMMSS();
} }
private getDDHHMMSS() {
const left = this._repeatCount - this._counter;
const days = Math.floor(left / 86400);
const dayMod = left % 86400;
const hours = Math.floor(dayMod / 3600);
const hoursMod = dayMod % 3600;
const minutes = Math.floor(hoursMod / 60);
const seconds = hoursMod % 60;
return {
dd: getTimeNum(days),
hh: getTimeNum(hours),
mm: getTimeNum(minutes),
ss: getTimeNum(seconds)
}
}
private getHHMMSS() { private getHHMMSS() {
const left = this._seconds - this._counter; const left = this._repeatCount - this._counter;
const hours = Math.floor(left / 3600); const hours = Math.floor(left / 3600);
const minutes = Math.floor((left % 3600) / 60); const minutes = Math.floor((left % 3600) / 60);
const seconds = left - hours * 3600 - minutes * 60; const seconds = left - hours * 3600 - minutes * 60;
return `${getTimeNum(hours)}:${getTimeNum(minutes)}:${getTimeNum(seconds)}`; return {
hh: getTimeNum(hours),
mm: getTimeNum(minutes),
ss: getTimeNum(seconds)
}
} }
private getMMSS() { private getMMSS() {
const left = this._seconds - this._counter; const left = this._repeatCount - this._counter;
const minutes = Math.floor(left / 60); const minutes = Math.floor(left / 60);
const seconds = left % 60; const seconds = left % 60;
return `${getTimeNum(minutes)}:${getTimeNum(seconds)}`; return {
} mm: getTimeNum(minutes),
ss: getTimeNum(seconds)
private getText() { }
return this._text.replace("{0}", this.getCountdown());
} }
start() { start() {
this._running = true; this._running = true;
this.emit("update", this.getText()); this.emit("update", this.getCountdown());
this._timer = setInterval(() => { this._timer = setInterval(() => {
this._counter++; this._counter++;
this.emit("update", this.getText()); this.emit("update", this.getCountdown());
if (this._counter == this._seconds) { if (this._counter == this._repeatCount) {
this.stop(); this.stop();
} }
}, this._delay); }, this._delay);
...@@ -66,7 +84,7 @@ export class Countdown extends EventEmitter<any> { ...@@ -66,7 +84,7 @@ export class Countdown extends EventEmitter<any> {
reset($seconds: number) { reset($seconds: number) {
clearInterval(this._timer); clearInterval(this._timer);
this._counter = 0; this._counter = 0;
this._seconds = $seconds; this._repeatCount = $seconds;
} }
// set seconds(val: number) { // set seconds(val: number) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment