-
-
Save mgold/d9b9f203affe01f1800e to your computer and use it in GitHub Desktop.
Restartable timer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SuperTimer where | |
import Html exposing (..) | |
import Html.Events exposing (onClick) | |
import Time exposing (Time) | |
import Signal exposing (Address) | |
type Action = Tick Time | Toggle Bool | NoOp | |
type alias Model = { running: Bool, count: Float} | |
model : Model | |
model = {running = False, count = 0} | |
view : Model -> Html.Html | |
view {running, count} = | |
div [] | |
[ div [] [ text (toString (count / 1000)) ] | |
, button | |
[ onClick mailbox.address | |
(if running then False else True) ] | |
[ text (if running then "Stop Timer" else "Start Timer") ] | |
] | |
mailbox = Signal.mailbox model.running | |
clicks : Signal Bool | |
clicks = mailbox.signal | |
ticks : Signal Action | |
ticks = Signal.map Tick (Time.fpsWhen 60 clicks) | |
actions : Signal Action | |
actions = Signal.merge (Signal.map Toggle clicks) ticks | |
update : Action -> Model -> Model | |
update action model = | |
case action of | |
Tick dt -> | |
if model.running then | |
{ model | count = model.count + dt } | |
else | |
model | |
Toggle b -> | |
{ model | running = not model.running } | |
NoOp -> | |
model | |
model_signal : Signal Model | |
model_signal = | |
Signal.foldp update model actions | |
main : Signal Html.Html | |
main = | |
Signal.map view model_signal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment