Registering New Elements
suggest changeDefines an <initially-hidden> custom element which hides its contents until a specified number of seconds have elapsed.
document.registerElement and createdCallback are the removed custom-elements v0 API. Define the element with customElements.define. connectedCallback can run again if the node is moved, so clear a previous timer first.
class InitiallyHiddenElement extends HTMLElement {
constructor() {
super();
this.revealTimeoutId = null;
}
connectedCallback() {
const seconds = Number(this.getAttribute("for"));
this.style.display = "none";
clearTimeout(this.revealTimeoutId);
this.revealTimeoutId = setTimeout(() => {
this.style.display = "block";
}, seconds * 1000);
}
disconnectedCallback() {
clearTimeout(this.revealTimeoutId);
this.revealTimeoutId = null;
}
}
customElements.define("initially-hidden", InitiallyHiddenElement);
<initially-hidden for="2">Hello</initially-hidden>
<initially-hidden for="5">World</initially-hidden>
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents