Home > database >  I was just start learn angular but I can't understand why we not work settimeout() outside the
I was just start learn angular but I can't understand why we not work settimeout() outside the

Time:10-20

export class ServerComponent implements OnInit { 

    show = false;

    setTimeout(() => {
        this.show = true;
    }, 2000);
  
    constructor(){
    }
    
    ngOnInit(){

    }
}

CodePudding user response:

You cannot put setTimeout or anything that isn't a property of method inside a class definition.

Make sure you understand Classes properly!

The right way to do this would be to put the setTimeout inside your constructor function

CodePudding user response:

Set timeout is basically a prebuilt library function (method) with two parameters.

  1. callback function
  2. timeout in milliseconds

so what you trying to do is calling a existing library function outside a method or constructor which is not allowed.

  • Related