Skip to content

Instantly share code, notes, and snippets.

@DarkWiiPlayer
Created August 3, 2021 14:33
Show Gist options
  • Save DarkWiiPlayer/fdb6701212505148448a020209093212 to your computer and use it in GitHub Desktop.
Save DarkWiiPlayer/fdb6701212505148448a020209093212 to your computer and use it in GitHub Desktop.
HTML Timer
<script type="module">
import {Better} from "/better.js"
class ConstantInterval extends Better {
#timer
static attributes = {
interval: { get: Number },
onTick: {
get: Function,
set: e => `(${e.toString()})()`
}
}
intervalChanged(t) {
if (this.#timer) window.clearInterval(this.#timer)
this.#timer = window.setInterval(() => this.tick(), Number(t)*1e3)
}
tick() {
this.onTick()
this.dispatchEvent(new CustomEvent("tick"))
}
}
ConstantInterval.initialize()
</script>
<constant-interval interval=2 on-tick="console.log('tick')"></constant-interval>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment