folk timer

This commit is contained in:
“chrisshank” 2024-11-22 23:05:51 -08:00
parent bbfcc029ce
commit 57253769b4
2 changed files with 43 additions and 33 deletions

View File

@ -109,43 +109,12 @@
<script type="module">
import { FolkGeometry } from '../src/canvas/fc-geometry.ts';
import { FolkLLM } from '../src/folk-llm.ts';
import { FolkTimer } from '../src/folk-timer.ts';
import { EventPropagator } from '../src/arrows/event-propagator.ts';
FolkGeometry.register();
FolkLLM.register();
customElements.define(
'folk-timer',
class Timer extends HTMLElement {
#timeMs = 0;
#intervalMs = 100;
#timeoutId = -1;
connectedCallback() {
this.#updateTime(0);
}
start() {
this.#timeoutId = setInterval(this.#updateTime, this.#intervalMs);
}
stop() {
clearInterval(this.#timeoutId);
this.#timeoutId = -1;
}
reset() {
this.stop();
this.#updateTime(0);
}
#updateTime = (time = this.#timeMs + this.#intervalMs) => {
this.#timeMs = time;
this.textContent = (time / 1000).toFixed(1);
};
}
);
FolkTimer.register();
EventPropagator.register();
</script>
</body>

41
src/folk-timer.ts Normal file
View File

@ -0,0 +1,41 @@
declare global {
interface HTMLElementTagNameMap {
'folk-timer': FolkTimer;
}
}
export class FolkTimer extends HTMLElement {
static tagName = 'folk-timer';
static register() {
customElements.define(this.tagName, this);
}
#timeMs = 0;
#timeoutId = -1;
#intervalMs = 100;
connectedCallback() {
this.#updateTime(0);
}
start() {
this.#timeoutId = setInterval(this.#updateTime, this.#intervalMs);
}
stop() {
clearInterval(this.#timeoutId);
this.#timeoutId = -1;
}
reset() {
this.stop();
this.#updateTime(0);
}
#updateTime = (time = this.#timeMs + this.#intervalMs) => {
this.#timeMs = time;
this.textContent = (time / 1000).toFixed(1);
};
}