TimeDistanceController
Purpose
A controller that displays the distance in words between the specified time and the current time. Works for both past and future dates.
Inspired by https://apidock.com/rails/ActionView/Helpers/DateHelper/distance_of_time_in_words, but updates in real time.
Implemented using https://date-fns.org/v2.21.1/docs/intervalToDuration, wrapped in intelligent timing code that automatically works out how often to update the API.
Fire and forget. The only configuration this controller takes is the timestamp.
Actions
Action | Purpose |
---|---|
- | - |
Targets
Target | Purpose | Default |
---|---|---|
- | - | - |
Classes
Class | Purpose | Default |
---|---|---|
- | - | - |
Values
Value | Type | Description | Default |
---|---|---|---|
timestamp | String | The UNIX timestamp in seconds of the date/time to show "time ago" for | - |
Events
Event | When | Dispatched on | event.detail |
---|---|---|---|
- | - | - | - |
Side Effects
The controller will set increasingly infrequent timeouts using setTimeout
according to the distance to the timestamp in timestampValue
. As the time gets further away, the controller updates the text lest frequently.
- If the timestamp was seconds/minutes ago, the controller will set timeouts to update the text every ~30 seconds.
- If the timestamp was hours ago, the controller will set timeouts to update the text every ~30 minutes.
- If the timestamp was days or more ago, the controller will not set any timeouts.
All timeouts are cleaned up when the controller disconnects.