setTimeout在Typescript / Angular中的forEach循环中不起作用

时间:2018-06-07 12:14:15

标签: javascript angular typescript

我有forEach循环,在10秒后隐藏HTML元素。我已将它放在setTimeout函数中,但它不等待10秒,该元素隐藏在页面加载中。 不确定是否因为Angulars生命周期外观或forEach循环。

这是我的打字稿代码



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




1 个答案:

答案 0 :(得分:4)

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

需要:

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

setTimeout需要一个函数。

相关问题