Home > Back-end >  Is there a way to create an HTMLElement, which will delete itself later?
Is there a way to create an HTMLElement, which will delete itself later?

Time:08-25

I have an abstract class "Component"

abstract class Component {
    protected container: HTMLElement;
  
    constructor(tagName: string, className: string) {
      this.container = document.createElement(tagName);
      this.container.className = className;
    }
  
    render() {
      return this.container;
    }
}

Other classes extend this class. Is there a way to make render() return a this.container, which later will be deleted from DOM, i.e. after 5 seconds?

CodePudding user response:

Have you tried using setTimeout?

abstract class Component {
    protected container: HTMLElement;
  
    constructor(tagName: string, className: string) {
      this.container = document.createElement(tagName);
      this.container.className = className;
    }
  
    render() {
      setTimeout(() => {
        this.container.remove()
      }, 5000)
      return this.container;
    }
}

CodePudding user response:

Phasor 3 kept leading me back to these three simple codes. Simply put, and im guessing. java = NOOP

  • Related