Skip to content

Instantly share code, notes, and snippets.

@shamrin
Last active February 14, 2016 16:12
Show Gist options
  • Save shamrin/79ec37c2d17b35c5773c to your computer and use it in GitHub Desktop.
Save shamrin/79ec37c2d17b35c5773c to your computer and use it in GitHub Desktop.
Restartable timer
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
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 (not running) ]
[ 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 = b }
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