Elapsed
Elapsed
The Module Flow.Elapsed module has a few functions for tracking passage of time.
An overview:
- Elapsed.since - time from a start point
- Elapsed.interval - time from start point, and between each subsequent call
- Elapsed.once - one-time measurement from a start point
Since
Elapsed.since yields how much time (in milliseconds) has passed since first invoked. This is the fixed reference point all later invocations are compared to.
Interval
Elapsed.interval reports the time from the first initialisation and each subsequent call. Unlike since
, there is a not a fixed reference point. It is always comparing to either the initial time or when the callback was last run.
Once
Elapsed.once fixes both the start point and a second reference time. After initialisation, it records the time at which the first call happens. This is then given as the value for all future calls.
Time completion
If you have a sense of how long something should take, you can track progression.
Flow.ofTotal takes an Interval and returns a function that reports back a 0..1 percentage of completion.
Since we could call the function after the duration has elapsed, it may well return a number over 1. To avoid this, clamp the value:
It’s also possible to wrap the value. When the total duration is exceeded, the progression wraps around back to zero. Therefore, an elapsed time of 150% is reported as 0.5, not 1.5.