on c19ccfe help sign in
elk/libraries/Time.elm
69 lines 2.4 kB history raw
1-- Library for working with time. Type `Time` represents some number of milliseconds.
2module Time where
4import Native.Time as T
6-- Type alias to make it clearer when you are working with time values.
7-- Using the `Time` constants instead of raw numbers is very highly recommended.
8type Time = Float
10-- Units of time, making it easier to specify things like a
11-- half-second `(500 * milliseconds)` without remembering Elm’s
12-- underlying units of time.
13millisecond : Time
14millisecond = 1
16second : Time
17second = 1000 * millisecond
19minute : Time
20minute = 60 * second
22hour : Time
23hour = 60 * minute
25inMilliseconds : Time -> Float
26inMilliseconds t = t
28inSeconds : Time -> Float
29inSeconds t = t / second
31inMinutes : Time -> Float
32inMinutes t = t / minute
34inHours : Time -> Float
35inHours t = t / hour
37-- Takes desired number of frames per second (fps). The resulting signal
38-- gives a sequence of time deltas as quickly as possible until it reaches
39-- the desired FPS. A time delta is the time between the last frame and the
40-- current frame.
41fps : Number a -> Signal Time
43-- Same as the fps function, but you can turn it on and off. Allows you
44-- to do brief animations based on user input without major inefficiencies.
45-- The first time delta after a pause is always zero, no matter how long
46-- the pause was. This way summing the deltas will actually give the amount
47-- of time that the output signal has been running.
48fpsWhen : Number a -> Signal Bool -> Signal Time
50-- Takes a time interval t. The resulting signal is the current time,
51-- updated every t.
52every : Time -> Signal Time
54-- Takes a time `t` and any signal. The resulting boolean signal
55-- is true for time `t` after every event on the input signal.
56-- So ``(second `since` Mouse.clicks)`` would result in a signal
57-- that is true for one second after each mouse click and false
58-- otherwise.
59since : Time -> Signal a -> Signal Bool
61-- Add a timestamp to any signal. Timestamps increase monotonically. Each
62-- timestamp is related to a specfic event, so `Mouse.x` and `Mouse.y` will
63-- always have the same timestamp because they both rely on the same
64-- underlying event.
65timestamp : Signal a -> Signal (Time, a)
67-- Delay a signal by a certain amount of time. So `(delay second Mouse.clicks)`
68-- will update one second later than any mouse click.
69delay : Time -> Signal a -> Signal a