Explanation behind my question
Every day, I am sent two timestamps through MQTT at 01:15 - these timestamps represent the beginning and end of a daily event (in this case, when my children are at school). It may not be the most exciting information for a home dashboard, but it serves its purpose.
https://i.sstatic.net/g4fJ4.png
Currently, my dashboard displays 9:00 → 17:10
, updating whenever new MQTT messages are received and the values for start
and end
change.
This functionality works well.
Now, I want to enhance the display to show additional information as follows:
9:00 → 17:10 (in 27 minutes)
The part in parentheses represents the time remaining until the end of the school day (17:10
). I have the necessary logic to compute and display this information, with the condition that it should only be visible when "now" is within the specified timeframe.
This is achieved by using the following code snippet:
{{ extractTime(enfant.start) }} → {{ extractTime(enfant.end) }} <span v-if="dansCreneau(enfant.start, enfant.end)">({{dansTime(enfant.end)}})</span>
extractTime
converts an ISO date to a 24-hour time formatdansCreneau
returnstrue
if "now" is between the specified dates, otherwise returnsfalse
dansTime
provides a human-readable representation of the time remaining until the end of the school day
The Issue I'm Facing
I can successfully display this information once, with a single calculation determining what goes inside the parentheses. However, from Vue's perspective, there is no compelling reason to recalculate it unless something changes.
I've considered using setInterval(XXX, 55000);
for continuous updates and I'm unsure about what XXX
should entail. Initially, I thought of reassigning enfant = enfant
within the interval function, but this wouldn't trigger a recalculation since the value of enfant
remains the same.
What would be the correct approach to force the recalculation and update of the remaining time?