<live-timer>
Exercise: <live-timer> element - JS.info
solution to the exercise
💾 replit
// 1. define new element
class LiveTimer extends HTMLElement {
/* ---------- life cycle callbacks ---------- */
// triggers when element is added to page
// (or when HTML parser detects it)
connectedCallback() {
this.render();
// ⭐️ start timer
this.startTimer();
}
disconnectedCallback() {
// ⭐️ important to let the element be garbage-collected
this.cancelTimer();
}
/* ---------- helper methods ---------- */
render() {
// show a nicely formatted time.
// ⭐️ 注意:這裡設定的東西屬於 light DOM❗️
this.innerHTML = `
<time-formatted
year="numeric"
month="long"
day="numeric"
hour="numeric"
minute="numeric"
second="numeric"
time-zone-name="short">
</time-formatted>
`;
// ⭐️ reference to <time-formatted> child
this.timeChild = this.firstElementChild;
}
// update
update(){
this.date = new Date();
// ⭐️ update child
this.timeChild.attr('datetime', this.date);
// ⭐️ dispatch 'tick'
let tick = new CustomEvent('tick', {
detail: this.date
});
this.dispatchEvent(tick);
}
// start timer
startTimer(){
// ⭐️ 注意:不要寫成「 setInterval(this.update, 1000) 」❗️
// 不然會失去 `this` context❗️
this.timer = setInterval(() => this.update(), 1000);
}
// cancel timer
cancelTimer(){
// ⭐️ 注意:不是 this.timer = null❗️
clearInterval(this.timer);
}
}
// 2. register new element
customElements.define("live-timer", LiveTimer);
Last updated
Was this helpful?