Skip to content

Instantly share code, notes, and snippets.

@Cspeisman
Last active April 22, 2016 11:37
Show Gist options
  • Save Cspeisman/5210ff69122e2a17cb15d4d4070a39b8 to your computer and use it in GitHub Desktop.
Save Cspeisman/5210ff69122e2a17cb15d4d4070a39b8 to your computer and use it in GitHub Desktop.
module Timer (..) where
import StartApp
import Graphics.Element
import Time
import Debug
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events
import Effects exposing (Effects)
-- MODEL
type alias Model =
{ seconds : Int, isRunning : Bool }
initialModel : Model
initialModel =
{ seconds = 0, isRunning = False }
-- UPDATE
type Action
= Increment
| PauseResume
update : Action -> Model -> (Model, Effects Action)
update action model =
case action of
Increment ->
if model.isRunning then
({ model | seconds = model.seconds + 1 }, Effects.none)
else
(model, Effects.none)
PauseResume ->
({ model | isRunning = not model.isRunning }, Effects.none)
-- VIEW
view : Signal.Address Action -> Model -> Html
view address model =
let
minute = toString (model.seconds // 60)
second = model.seconds % 60
time = minute ++ ": " ++ (if second < 10 then ("0" ++ toString second) else (toString second))
in
Html.div
[ ]
[ Html.text time
, Html.button [ Html.Events.onClick address PauseResume ] [ Html.text (if model.isRunning then "stop" else "start") ]
]
app : StartApp.App Model
app =
StartApp.start {
init = (initialModel, Effects.none)
, view = view
, update = update
, inputs = [ Signal.map (\_ -> Increment) (Time.every Time.second) ]
}
main : Signal Html.Html
main =
app.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment