如何在" SetTimeout"内生成一个值?在它之外

时间:2018-04-05 07:01:29

标签: angular typescript settimeout angular2-directives

我有以下代码

setTimeout(()=>{ console.log("TIMED OUT DATA", this.fetchedData['__zone_symbol__value']) },3000);

我希望在setTimeout之外获得this.fetchedData['__zone_symbol__value']的值。它甚至可能吗?

1 个答案:

答案 0 :(得分:1)

只需在外面宣布并直接引用它。

here, in this working example

export class App {

  myData = false;

  constructor() {
   this.restart();
  }

  restart(){
    console.log("Changind data to false outside timeout");
    this.myData = false;

    setTimeout(()=>{ 
      console.log("TIMED OUT DATA", this.myData) 
      this.myData = true;
      console.log("TIMED OUT DATA changed", this.myData) 
    },3000);
  }
}