Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}
like image 289
Pradeep Balasubramaniam Avatar asked Nov 26 '25 00:11

Pradeep Balasubramaniam


2 Answers

I think you write the wrong syntax this code

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

should be like this

setTimeout(()=>{ this.fetchRequests(this); },1000);
like image 98
mostafa tourad Avatar answered Nov 27 '25 13:11

mostafa tourad


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);
    });
}
like image 33
wilsonhobbs Avatar answered Nov 27 '25 13:11

wilsonhobbs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!