user8053092
user8053092

Reputation: 165

setTimeout does not work inside forEach loop in Typescript / Angular

I have forEach loop that hides an HTML element after 10 sec. I've put it inside a setTimeout function but it's not waiting for 10 sec, the element is hiding with page load. Not sure if it is because of Angulars lifecycle looks or forEach loop.

Here is my typescript code

ngAfterViewChecked() {  
    if(this.on) {  
      this._notificationsElements.forEach((element) => {      
        const htmlElement = element.nativeElement as HTMLElement;
        setTimeout(htmlElement.setAttribute("style", "display:none;"), 10000);
      });
    }
  }

Upvotes: 1

Views: 192

Answers (1)

Joe
Joe

Reputation: 82614

setTimeout(htmlElement.setAttribute("style", "display:none;"),10000);

needs to be:

setTimeout(() => htmlElement.setAttribute("style", "display:none;"),10000);

setTimeout requires a function.

Upvotes: 4

Related Questions