Aspnetcore-angular-universal: Using setInterval with Angular Universal

Created on 7 Apr 2018  路  7Comments  路  Source: TrilonIO/aspnetcore-angular-universal

How to use timer to update some data, (eg stock price)

FAQ HOW TO - Guide

Most helpful comment

Hello @arsenlol ,
have you tried using NgZone? For Example:

constructor(private zone: NgZone) { this.zone.runOutsideAngular(() => { this.someInterval = setInterval(() => { // Your Code }, 1000) })

Please let me know of this works for you...

All 7 comments

If you're trying to create an interval, do it as you normally would.

setInterval(() => { /* stuff */, 500); // example iterating every 500ms

Just make sure you have this wrapped in a conditional logic so that it _only_ runs in the PlatformBrowser. (More on that here: https://github.com/MarkPieszak/aspnetcore-angular2-universal/blob/master/README.md#gotchas)

I have tried using setInterval

if (isPlatformBrowser(this.platformId)) {
setInterval(() => {
this.next();
}, 5000);

}

when loading the home component, it didnt load,(having blank screen)
but when i switch page and coming back , it loads,

if i remove the setInterval function , everything work normally

Try this:

if (isPlatformBrowser(this.platformId)) {
let self = this;
setInterval(() => {
self.next();
}, 5000);
}

See self.next() instead of this.next()

I should also recommend clearing that interval on ngOnDestroy method.

@Flood , still same using self or this

same issue here, why is this closed?

Hello @arsenlol ,
have you tried using NgZone? For Example:

constructor(private zone: NgZone) { this.zone.runOutsideAngular(() => { this.someInterval = setInterval(() => { // Your Code }, 1000) })

Please let me know of this works for you...

@StephanMullis I got it working. Thank you!

Was this page helpful?
0 / 5 - 0 ratings