Skip to main content

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

ActionPurpose
--

Targets

TargetPurposeDefault
---

Classes

ClassPurposeDefault
---

Values

ValueTypeDescriptionDefault
timestampStringThe UNIX timestamp in seconds of the date/time to show "time ago" for-

Events

EventWhenDispatched onevent.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.

How to Use