angular - Cómo obtener un valor generado dentro de "SetTimeout" fuera de él
Tengo el siguiente fragmento de código
setTimeout(()=>{ console.log("TIMED OUT DATA", this.fetchedData['__zone_symbol__value']) },3000);
Quiero obtener el valor de this.fetchedData['__zone_symbol__value'] fuera del setTimeout. ¿Es siquiera posible?
Respuesta #1
Simplemente decláralo fuera y haz referencia a él directamente.
Como aquí, en este ejemplo funcional.
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);
}
}
