primrose
Version:
Syntax-highlighting text editor that renders to an HTML5 Canvas element
51 lines (42 loc) • 1.23 kB
JavaScript
import { EventBase } from "./eventBase.js";
export class TimedEvent extends EventBase {
constructor(timeout, continuous = false) {
super();
const tickEvt = new Event("tick");
let handle = null;
this.cancel = () => {
const wasRunning = this.isRunning;
if (wasRunning) {
if (continuous) {
clearInterval(handle);
}
else {
clearTimeout(handle);
}
handle = null;
}
return wasRunning;
};
const tick = () => {
if (!continuous) {
this.cancel();
}
this.dispatchEvent(tickEvt);
};
this.start = () => {
this.cancel();
if (continuous) {
handle = setTimeout(tick, timeout);
}
else {
handle = setInterval(tick, timeout);
}
};
Object.defineProperties(this, {
isRunning: {
get: () => handle !== null
}
});
Object.freeze(this);
}
}