Live timer element
We already have
<time-formatted> element to show a nicely formatted time.
<live-timer> element to show the current time:
- It should use
<time-formatted>internally, not duplicate its functionality.
- Ticks (updates) every second.
- For every tick, a custom event named
tickshould be generated, with the current date in
event.detail(see chapter Dispatching custom events).
<live-timer id="elem"></live-timer> <script> elem.addEventListener('tick', event => console.log(event.detail)); </script>
- We clear
setIntervaltimer when the element is removed from the document. That’s important, otherwise it continues ticking even if not needed any more. And the browser can’t clear the memory from this element and referenced by it.
- We can access current date as
elem.dateproperty. All class methods and properties are naturally element methods and properties.