I want to display a relative timestamp. In the Angular template I have
{{relativeTime}}
and in the component I get the input as
timeStamp = input.required<Date>();
The calculation of the relative time is done like
relativeTime='Just now';
updateRelativeTime(): void {
if (Date.now() - this.timeStamp().getTime() > 30000) {
this.relativeTime= DateTime.fromJSDate(this.timeStamp()).toRelative() ?? '';
}
}
So far the code works as expected if I stich it together. Additionally I have in the constructor
an update every 5 seconds (in the first minute)
timer(30000, 5000)
.pipe(take((60 - 30) / 5))
.subscribe(() => {
this.updateRelativeTime();
}),
My problem is if the timeStamp
changes I want to have a new calculation. I was thinking about the new Angular 19 feature with linkedSignal
but somehow I don't see how they come togehter :-/ I can do an inital computation and set something like
relativeTime = linkedSignal(() => updateRelativeTime());
But than it would be the same as computed. The problem with computed
is that I need to updated it via interval
... So, how to do it properly?