When i use setInterval in Angular the browser doesnt load the content anymore. This problem just happens in Angular 17. I dont know if im the only one with this or if nobody in Google or in Angular community notice, that there is a problem with the setInterval.
import { AfterViewInit, Component, OnInit, inject } from '@angular/core';
import { CarService } from '../services/cars.service';
import { Car } from '../interfaces/car';
@Component({
selector: 'app-count-cars',
standalone: true,
imports: [],
templateUrl: './count-cars.component.html',
styleUrl: './count-cars.component.scss'
})
export class CountCarsComponent implements AfterViewInit {
carService: CarService = inject(CarService)
cars: Car[] = []
carsNumber = 0;
constructor() {
this.cars = this.carService.getCars();
this.carsNumber = this.cars.length;
}
ngAfterViewInit(): void {
setInterval(()=> {
this.cars = this.carService.getCars();
this.carsNumber = this.cars.length;
}, 10000)
}
}
Everything works well without setIntervall.
Your builder is probably building a server-side bundle too which will get stuck if you use any
setIntervalwithout checking if the platform is abrowser. The server run will infinitely run the interval and will not proceed.Wrap your interval inside an
ifblock with a condition to check that the platform is or is not abrowser.try this: