My goal is to implement polling for a single page and disable it when the user navigates away from that page. However, I encountered an error during the build process when I attempted to set up the polling interval using setInterval:
error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.
Below is the code for the CommandPageComponent class:
export class CommandPageComponent implements OnInit, OnDestroy {
private lastUpdate: number;
private timer: any;
private pollingTimer: any;
private seconds = 0;
constructor(private router: Router, private consultService: ConsultService, private configService: ConfigService) {
this.configService.getConfigData('pollingEnabled').subscribe(enabled => {
if (enabled) {
let j = 0;
this.configService.getConfigData('pollingInterval').subscribe(interval => {
this.pollingTimer = setInterval(() => {
console.log('polling', ++j);
console.log('polling', interval);
this.seconds = 0;
this.consultService.sendConsultPollingRequest();
}, interval * 1000);
this.timer = setInterval(() => {
this.lastUpdate = interval - this.seconds++;
}, 1000);
});
}else {
console.log('Polling disabled');
}
});
}
ngOnDestroy() {
// Will clear when component is destroyed e.g. route is navigated away from
if (this.pollingTimer) {
clearInterval(this.pollingTimer);
}
if (this.timer) {
clearInterval(this.timer);
}
console.log('Clear polling');
}
}
Can anyone help identify where the issue might be occurring? Thank you.