I am working on implementing a property called currentAndLastVehicles
in my service that needs to be updated automatically every second.
Here is what I have so far:
import { Injectable } from '@angular/core';
@Injectable()
export class SharedDataService {
constructor() {
setTimeout(() => {
console.log("hello");
this.currentAndLastVehicles = [(Math.floor(Math.random() * (1000000 - 9999999)) + 9999999).toString(), ...this.currentAndLastVehicles];
}, 1000);
}
public currentAndLastVehicles: string[] = [];
}
Challenges I am facing:
- The setTimeout function is only executed once, probably because it is inside the constructor.
- Placing setTimeout outside of the constructor results in multiple errors.
Can anyone suggest a solution to achieve the desired functionality?