Commit e66c474a authored by AU-PRO's avatar AU-PRO

init and beta 1.0 for test, author id AUPRO --3

parent 0bb08a9f
import EventEmitter from "./EventEmitter";
export default class Countdown extends EventEmitter<any> {
private _timer;
private _text;
private _seconds;
private _format;
private _counter;
private _delay;
private _running;
constructor(text: any, seconds: any, format: any, delay: any);
private getCountdown;
private getHHMMSS;
private getMMSS;
private getText;
start(): void;
private stop;
reset($seconds: number): void;
get running(): boolean;
}
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var EventEmitter_1 = require("./EventEmitter");
function getNum(n) {
return n >= 10 ? "" + n : "0" + n;
}
var Countdown = /** @class */ (function (_super) {
__extends(Countdown, _super);
function Countdown(text, seconds, format, delay) {
var _this = _super.call(this) || this;
_this._running = false;
_this._text = text;
_this._seconds = seconds;
_this._format = format;
_this._counter = 0;
_this._delay = delay || 1000;
return _this;
}
Countdown.prototype.getCountdown = function () {
if (this._format == "hhmmss")
return this.getHHMMSS();
if (this._format == "mmss")
return this.getMMSS();
};
Countdown.prototype.getHHMMSS = function () {
var left = this._seconds - this._counter;
var hours = Math.floor(left / 3600);
var minutes = Math.floor((left % 3600) / 60);
var seconds = left - hours * 3600 - minutes * 60;
return getNum(hours) + ":" + getNum(minutes) + ":" + getNum(seconds);
};
Countdown.prototype.getMMSS = function () {
var left = this._seconds - this._counter;
var minutes = Math.floor(left / 60);
var seconds = left % 60;
return getNum(minutes) + ":" + getNum(seconds);
};
Countdown.prototype.getText = function () {
return this._text.replace("{0}", this.getCountdown());
};
Countdown.prototype.start = function () {
var _this = this;
this._running = true;
this.emit("update", this.getText());
this._timer = setInterval(function () {
_this._counter++;
_this.emit("update", _this.getText());
if (_this._counter == _this._seconds) {
_this.stop();
}
}, this._delay);
};
Countdown.prototype.stop = function () {
this._running = false;
clearInterval(this._timer);
this.emit("complete");
};
Countdown.prototype.reset = function ($seconds) {
this._counter = 0;
this._seconds = $seconds;
};
Object.defineProperty(Countdown.prototype, "running", {
// set seconds(val: number) {
// this._seconds = val;
// }
get: function () {
return this._running;
},
enumerable: false,
configurable: true
});
return Countdown;
}(EventEmitter_1.default));
exports.default = Countdown;
export default class EventEmitter<T> {
private events;
on(event: T, fn: Function, context?: any): this;
once(event: T, fn: Function, context?: any): this;
off(event: T, fn?: Function, context?: any, once?: boolean): this;
offAll(): this;
emit(event: T, ...args: any[]): this;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var EventEmitter = /** @class */ (function () {
function EventEmitter() {
this.events = new Map();
}
EventEmitter.prototype.on = function (event, fn, context) {
var listeners = this.events.get(event);
if (listeners) {
listeners.push({ once: false, fn: fn, context: context });
}
else {
this.events.set(event, [{ once: false, fn: fn, context: context }]);
}
return this;
};
EventEmitter.prototype.once = function (event, fn, context) {
var listeners = this.events.get(event);
if (listeners) {
listeners.push({ once: true, fn: fn, context: context });
}
else {
this.events.set(event, [{ once: true, fn: fn, context: context }]);
}
return this;
};
EventEmitter.prototype.off = function (event, fn, context, once) {
var listeners = this.events.get(event);
if (listeners) {
if (fn) {
for (var i = 0; i < listeners.length; i++) {
var l = listeners[i];
if ((fn === l.fn) && (!once || l.once) && (!context || l.context === context)) {
listeners.splice(i--, 1);
}
}
}
else {
this.events.delete(event);
}
}
return this;
};
EventEmitter.prototype.offAll = function () {
this.events.clear();
return this;
};
EventEmitter.prototype.emit = function (event) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
var listeners = this.events.get(event);
if (listeners) {
for (var i = 0; i < listeners.length; i++) {
var listener = listeners[i];
if (listener.once)
listeners.splice(i--, 1);
listener.fn.apply(listener.context, args);
}
}
return this;
};
return EventEmitter;
}());
exports.default = EventEmitter;
export declare const getLocalCache: (key: string) => boolean;
export declare const setLocalCache: (key: string) => boolean;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.setLocalCache = exports.getLocalCache = void 0;
exports.getLocalCache = function (key) {
if (localStorage && localStorage.getItem)
return localStorage.getItem(key) === '1';
return false;
};
exports.setLocalCache = function (key) {
if (localStorage && localStorage.setItem) {
localStorage.setItem(key, '1');
return true;
}
return false;
};
export declare class EventEmitter<T> {
private events;
on(event: T, fn: Function, context?: any): this;
once(event: T, fn: Function, context?: any): this;
off(event: T, fn?: Function, context?: any, once?: boolean): this;
offAll(): this;
emit(event: T, ...args: any[]): this;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventEmitter = void 0;
var EventEmitter = /** @class */ (function () {
function EventEmitter() {
this.events = new Map();
}
EventEmitter.prototype.on = function (event, fn, context) {
var listeners = this.events.get(event);
if (listeners) {
listeners.push({ once: false, fn: fn, context: context });
}
else {
this.events.set(event, [{ once: false, fn: fn, context: context }]);
}
return this;
};
EventEmitter.prototype.once = function (event, fn, context) {
var listeners = this.events.get(event);
if (listeners) {
listeners.push({ once: true, fn: fn, context: context });
}
else {
this.events.set(event, [{ once: true, fn: fn, context: context }]);
}
return this;
};
EventEmitter.prototype.off = function (event, fn, context, once) {
var listeners = this.events.get(event);
if (listeners) {
if (fn) {
for (var i = 0; i < listeners.length; i++) {
var l = listeners[i];
if (fn === l.fn &&
(!once || l.once) &&
(!context || l.context === context)) {
listeners.splice(i--, 1);
}
}
}
else {
this.events.delete(event);
}
}
return this;
};
EventEmitter.prototype.offAll = function () {
this.events.clear();
return this;
};
EventEmitter.prototype.emit = function (event) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
var listeners = this.events.get(event);
if (listeners) {
for (var i = 0; i < listeners.length; i++) {
var listener = listeners[i];
if (listener.once)
listeners.splice(i--, 1);
listener.fn.apply(listener.context, args);
}
}
return this;
};
return EventEmitter;
}());
exports.EventEmitter = EventEmitter;
export * from "./event";
export * from "./cache";
export * from "./time";
export * from "./svga";
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./event"), exports);
__exportStar(require("./cache"), exports);
__exportStar(require("./time"), exports);
__exportStar(require("./svga"), exports);
import SVGA = require("svgaplayerweb");
export declare const loadSVGA: (url: string) => Promise<unknown>;
export declare const getSVGA: (url: string, className: string, loops: number, clearsAfterStop: boolean) => Promise<SVGA.Player>;
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSVGA = exports.loadSVGA = void 0;
var SVGA = require("svgaplayerweb");
var svgaParser = new SVGA.Parser();
exports.loadSVGA = function (url) {
return new Promise(function (resolve, reject) {
svgaParser.load(url, function (videoItem) {
resolve(videoItem);
}, function (err) {
reject(err);
});
});
};
exports.getSVGA = function (url, className, loops, clearsAfterStop) { return __awaiter(void 0, void 0, void 0, function () {
var videoItem, svga;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, exports.loadSVGA(url)];
case 1:
videoItem = _a.sent();
svga = new SVGA.Player(className);
if (loops)
svga.loops = loops;
if (clearsAfterStop)
svga.clearsAfterStop = false;
svga.setVideoItem(videoItem);
svga.startAnimation();
return [2 /*return*/, svga];
}
});
}); };
import { EventEmitter } from "../event";
export declare class Countdown extends EventEmitter<any> {
private _timer;
private _text;
private _seconds;
private _format;
private _counter;
private _delay;
private _running;
constructor(text: any, seconds: any, format: any, delay: any);
private getCountdown;
private getHHMMSS;
private getMMSS;
private getText;
start(): void;
private stop;
reset($seconds: number): void;
get running(): boolean;
}
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Countdown = void 0;
var event_1 = require("../event");
function getTimeNum(n) {
return n >= 10 ? "" + n : "0" + n;
}
var Countdown = /** @class */ (function (_super) {
__extends(Countdown, _super);
function Countdown(text, seconds, format, delay) {
var _this = _super.call(this) || this;
_this._running = false;
_this._text = text;
_this._seconds = seconds;
_this._format = format;
_this._counter = 0;
_this._delay = delay || 1000;
return _this;
}
Countdown.prototype.getCountdown = function () {
if (this._format == "hhmmss")
return this.getHHMMSS();
if (this._format == "mmss")
return this.getMMSS();
};
Countdown.prototype.getHHMMSS = function () {
var left = this._seconds - this._counter;
var hours = Math.floor(left / 3600);
var minutes = Math.floor((left % 3600) / 60);
var seconds = left - hours * 3600 - minutes * 60;
return getTimeNum(hours) + ":" + getTimeNum(minutes) + ":" + getTimeNum(seconds);
};
Countdown.prototype.getMMSS = function () {
var left = this._seconds - this._counter;
var minutes = Math.floor(left / 60);
var seconds = left % 60;
return getTimeNum(minutes) + ":" + getTimeNum(seconds);
};
Countdown.prototype.getText = function () {
return this._text.replace("{0}", this.getCountdown());
};
Countdown.prototype.start = function () {
var _this = this;
this._running = true;
this.emit("update", this.getText());
this._timer = setInterval(function () {
_this._counter++;
_this.emit("update", _this.getText());
if (_this._counter == _this._seconds) {
_this.stop();
}
}, this._delay);
};
Countdown.prototype.stop = function () {
this._running = false;
clearInterval(this._timer);
this.emit("complete");
};
Countdown.prototype.reset = function ($seconds) {
this._counter = 0;
this._seconds = $seconds;
};
Object.defineProperty(Countdown.prototype, "running", {
// set seconds(val: number) {
// this._seconds = val;
// }
get: function () {
return this._running;
},
enumerable: false,
configurable: true
});
return Countdown;
}(event_1.EventEmitter));
exports.Countdown = Countdown;
import { EventEmitter } from "../event";
export declare class Timer extends EventEmitter<any> {
_running: boolean;
repeatCount: number;
delay: number;
currentCount: number;
_timer: number | undefined;
constructor(repeatCount: number, delay?: number);
start(): void;
stop(): void;
reset(): void;
}
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Timer = void 0;
var event_1 = require("../event");
var Timer = /** @class */ (function (_super) {
__extends(Timer, _super);
function Timer(repeatCount, delay) {
if (delay === void 0) { delay = 1000; }
var _this = _super.call(this) || this;
_this._running = false;
_this.repeatCount = repeatCount;
_this.delay = delay;
_this.currentCount = 0;
return _this;
}
Timer.prototype.start = function () {
var _this = this;
this._running = true;
this._timer = setInterval(function () {
_this.currentCount++;
if (_this.currentCount == _this.repeatCount) {
_this.stop();
_this.emit("complete");
}
}, this.delay);
};
Timer.prototype.stop = function () {
this._running = false;
clearInterval(this._timer);
};
Timer.prototype.reset = function () {
this.stop();
this.currentCount = 0;
this.start();
};
return Timer;
}(event_1.EventEmitter));
exports.Timer = Timer;
declare const _default: () => number;
export default _default;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = (function () {
var now = new Date();
now.setHours(0, 0, 0, 0);
return now.getTime();
});
export * from "./Countdown";
export * from "./Timer";
export * from "./tools";
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./Countdown"), exports);
__exportStar(require("./Timer"), exports);
__exportStar(require("./tools"), exports);
export declare const loadSVGA: (url: any) => Promise<unknown>;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadSVGA = void 0;
var SVGA = require("svgaplayerweb");
var parser = new SVGA.Parser();
exports.loadSVGA = function (url) {
return new Promise(function (resolve, reject) {
parser.load(url, function (videoItem) {
console.log("loaded", url);
resolve(videoItem);
}, function (err) {
reject(err);
});
});
};
export declare const getDateTimer: () => number;
/**
*
* @param ms
*/
export declare const wait: (ms: number) => Promise<unknown>;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.wait = exports.getDateTimer = void 0;
exports.getDateTimer = function () {
var now = new Date();
now.setHours(0, 0, 0, 0);
return now.getTime();
};
/**
*
* @param ms
*/
exports.wait = function (ms) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve();
}, ms);
});
};
import { getLocalCache, setLocalCache } from "./localCache";
import wait from "./wait";
export { wait, getLocalCache, setLocalCache };
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.setLocalCache = exports.getLocalCache = exports.wait = void 0;
var localCache_1 = require("./localCache");
Object.defineProperty(exports, "getLocalCache", { enumerable: true, get: function () { return localCache_1.getLocalCache; } });
Object.defineProperty(exports, "setLocalCache", { enumerable: true, get: function () { return localCache_1.setLocalCache; } });
var wait_1 = require("./wait");
exports.wait = wait_1.default;
export declare const getLocalCache: (key: any) => boolean;
export declare const setLocalCache: (key: any) => boolean;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.setLocalCache = exports.getLocalCache = void 0;
exports.getLocalCache = function (key) {
if (localStorage && localStorage.getItem)
return localStorage.getItem(key) === '1';
return false;
};
exports.setLocalCache = function (key) {
if (localStorage && localStorage.setItem) {
localStorage.setItem(key, '1');
return true;
}
return false;
};
declare const _default: (ms: any) => Promise<unknown>;
export default _default;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = (function (ms) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve();
}, ms);
});
});
declare const _default: (ms: any) => Promise<unknown>;
export default _default;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = (function (ms) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve();
}, ms);
});
});
const gulp = require('gulp')
const typedoc = require('gulp-typedoc')
const browserSync = require('browser-sync').create()
const runTypeDoc = () => gulp
.src(['src'])
.pipe(typedoc({
out: './docs',
mode:'file',
exclude: '**/*/index.ts',
tsconfig: 'tsconfig.json',
}))
const reload = (done) => {
browserSync.reload()
done()
}
const runBrowserSync = (done) => {
browserSync.init({
server: {
baseDir: './docs',
},
})
done()
}
const watch = () => gulp.watch(
['README.md', 'src/*.ts'],
gulp.series(runTypeDoc, reload)
)
gulp.task('watch', gulp.series(runTypeDoc, runBrowserSync, watch))
gulp.task('build', gulp.series(runTypeDoc))
\ No newline at end of file
This diff is collapsed.
{
"name": "teddi",
"version": "1.0.13",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"doc": "gulp build",
"watch": "gulp watch",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git@gitlab2.dui88.com:wanghongyuan/teddi.git"
},
"author": "",
"license": "ISC",
"dependencies": {
"svgaplayerweb": "^2.3.1"
},
"devDependencies": {
"browser-sync": "^2.26.13",
"gulp": "^4.0.2",
"gulp-typedoc": "^2.2.5",
"typedoc": "^0.19.2",
"typescript": "^4.0.5"
}
}
export const getLocalCache = (key:string) => {
if (localStorage && localStorage.getItem)
return localStorage.getItem(key) === '1'
return false;
}
export const setLocalCache = (key:string) => {
if (localStorage && localStorage.setItem) {
localStorage.setItem(key, '1');
return true;
}
return false;
}
\ No newline at end of file
export class EventEmitter<T> {
private events = new Map<
T,
{ fn: Function; context: any; once: boolean }[]
>();
public on(event: T, fn: Function, context?: any): this {
let listeners = this.events.get(event);
if (listeners) {
listeners.push({ once: false, fn, context });
} else {
this.events.set(event, [{ once: false, fn, context }]);
}
return this;
}
public once(event: T, fn: Function, context?: any): this {
let listeners = this.events.get(event);
if (listeners) {
listeners.push({ once: true, fn, context });
} else {
this.events.set(event, [{ once: true, fn, context }]);
}
return this;
}
public off(event: T, fn?: Function, context?: any, once?: boolean): this {
let listeners = this.events.get(event);
if (listeners) {
if (fn) {
for (let i = 0; i < listeners.length; i++) {
let l = listeners[i];
if (
fn === l.fn &&
(!once || l.once) &&
(!context || l.context === context)
) {
listeners.splice(i--, 1);
}
}
} else {
this.events.delete(event);
}
}
return this;
}
public offAll(): this {
this.events.clear();
return this;
}
public emit(event: T, ...args: any[]): this {
let listeners = this.events.get(event);
if (listeners) {
for (let i = 0; i < listeners.length; i++) {
let listener = listeners[i];
if (listener.once) listeners.splice(i--, 1);
listener.fn.apply(listener.context, args);
}
}
return this;
}
}
export * from "./event";
export * from "./cache";
export * from "./time";
export * from "./svga";
import SVGA = require("svgaplayerweb");
var svgaParser = new SVGA.Parser();
export const loadSVGA = (url:string) => {
return new Promise((resolve, reject) => {
svgaParser.load(url, (videoItem:SVGA.VideoEntity) => {
resolve(videoItem)
}, (err) => {
reject(err)
})
})
}
export const getSVGA = async (url:string, className:string, loops:number, clearsAfterStop:boolean) => {
const videoItem = await loadSVGA(url) as SVGA.VideoEntity;
var svga = new SVGA.Player(className);
if (loops)
svga.loops = loops;
if (clearsAfterStop)
svga.clearsAfterStop = false;
svga.setVideoItem(videoItem);
svga.startAnimation();
return svga
}
\ No newline at end of file
import { EventEmitter } from "../event";
export class Timer extends EventEmitter<any> {
_running = false;
repeatCount;
delay;
currentCount;
_timer: number | undefined;
constructor(repeatCount: number, delay = 1000) {
super();
this.repeatCount = repeatCount;
this.delay = delay;
this.currentCount = 0;
}
start() {
this._running = true;
this._timer = setInterval(() => {
this.currentCount++;
if (this.currentCount == this.repeatCount) {
this.stop();
this.emit("complete");
}
}, this.delay);
}
stop() {
this._running = false;
clearInterval(this._timer);
}
reset() {
this.stop();
this.currentCount = 0;
this.start();
}
}
import { EventEmitter } from "../event";
function getTimeNum(n: any) {
return n >= 10 ? `${n}` : `0${n}`;
}
export class Countdown extends EventEmitter<any> {
private _timer: any;
private _text: any;
private _seconds: any;
private _format: any;
private _counter: any;
private _delay: any;
private _running = false;
constructor(text: any, seconds: any, format: any, delay: any) {
super();
this._text = text;
this._seconds = seconds;
this._format = format;
this._counter = 0;
this._delay = delay || 1000;
}
private getCountdown() {
if (this._format == "hhmmss") return this.getHHMMSS();
if (this._format == "mmss") return this.getMMSS();
}
private getHHMMSS() {
const left = this._seconds - this._counter;
const hours = Math.floor(left / 3600);
const minutes = Math.floor((left % 3600) / 60);
const seconds = left - hours * 3600 - minutes * 60;
return `${getTimeNum(hours)}:${getTimeNum(minutes)}:${getTimeNum(seconds)}`;
}
private getMMSS() {
const left = this._seconds - this._counter;
const minutes = Math.floor(left / 60);
const seconds = left % 60;
return `${getTimeNum(minutes)}:${getTimeNum(seconds)}`;
}
private getText() {
return this._text.replace("{0}", this.getCountdown());
}
start() {
this._running = true;
this.emit("update", this.getText());
this._timer = setInterval(() => {
this._counter++;
this.emit("update", this.getText());
if (this._counter == this._seconds) {
this.stop();
}
}, this._delay);
}
private stop() {
this._running = false;
clearInterval(this._timer);
this.emit("complete");
}
reset($seconds: number) {
this._counter = 0;
this._seconds = $seconds;
}
// set seconds(val: number) {
// this._seconds = val;
// }
get running() {
return this._running;
}
}
export * from "./Countdown";
export * from "./Timer";
export * from "./tools";
export const getDateTimer = () => {
const now = new Date();
now.setHours(0, 0, 0, 0);
return now.getTime();
};
/**
*
* @param ms
*/
export const wait = (ms:number) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, ms);
});
};
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