setTimeout not working with Angular5

this.fetchRequests(this) gets executed as soon as the view is initialized instead of waiting for 20 seconds.

I am aware in the older versions we have to use the wrapper but in the new versions those are not required I guess. Any help on this would be helpful.

ngAfterViewInit() {
    setTimeout(()=>{
        this.fetchRequests(this);
    }), 10000;
}

Upvotes: 3

Views: 8783

Answers (3)

Vikas Kandari
Vikas Kandari

Reputation: 1851

your syntax is wrong

two syntax's are here for function one which is blow new one

setTimeout(()=>{
    this.fetchRequests(this); 
},1000);

and second below syntax is old one you can use both of them

setTimeout(function(){
        this.fetchRequests(this); 
},1000);

both are functional in Angular 9- Typescript and Nodejs and latest javascript version by now

Upvotes: 1

mostafa tourad
mostafa tourad

Reputation: 4388

I think you write the wrong syntax this code

setTimeout(()=>{
    this.fetchRequests(this);
}), 10000;

should be like this

setTimeout(()=>{ this.fetchRequests(this); },1000);

Upvotes: 9

wilsonhobbs
wilsonhobbs

Reputation: 951

Wrap it in zone.run().

First, import NgZone from @angular/core, and inject it into your component.

Then, modify your ngAfterViewInit to look like this:

ngAfterViewInit() {
    this.zone.run(() => {
        setTimeout(()=>{
            this.fetchRequests(this);
        }, 10000);
    });
}

Upvotes: 4

Related Questions