Registering New Elements

suggest change

Defines 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>

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents