-
Notifications
You must be signed in to change notification settings - Fork 0
/
script2.js
35 lines (30 loc) · 988 Bytes
/
script2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class MeuRelogioElement extends HTMLElement {
constructor() {
super()
console.log("constructor") //o constructor é chamado só uma vez, quando o elemento é construído
this.attachShadow({ mode: "open" })
this.spanEl = document.createElement("span")
this.spanEl.textContent = getHMS()
this.shadowRoot.append(this.spanEl)
}
connectedCallback() {
this.spanEl.textContent = getHMS()
this.timer = setInterval(() => {
this.spanEl.textContent = getHMS()
}, 1000)
}
disconnectedCallback() {
clearInterval(this.timer)
}
}
function getHMS() {
const dh = new Date()
const h = formatNumber(dh.getHours())
const m = formatNumber(dh.getMinutes())
const s = formatNumber(dh.getSeconds())
return `${h}:${m}:${s}`
}
function formatNumber(n) {
return String(n).padStart(2, "0")
}
customElements.define("meu-relogio", MeuRelogioElement)