diff --git a/demo/llm.html b/demo/llm.html index 248202e..28eab7b 100644 --- a/demo/llm.html +++ b/demo/llm.html @@ -109,43 +109,12 @@ diff --git a/src/folk-timer.ts b/src/folk-timer.ts new file mode 100644 index 0000000..fcaa7f6 --- /dev/null +++ b/src/folk-timer.ts @@ -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); + }; +}