Skip to content

Instantly share code, notes, and snippets.

@sectore
Created August 1, 2016 20:39
Show Gist options
  • Save sectore/be2700e0b0b071878695d42baeb238b3 to your computer and use it in GitHub Desktop.
Save sectore/be2700e0b0b071878695d42baeb238b3 to your computer and use it in GitHub Desktop.
How to run `Time.now` with `init`
module Main exposing (..)
import Html exposing (..)
import Html.App as Html
import Time
import Basics.Extra exposing (never)
import Task
main : Program Never
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- MODEL
type alias Model =
{ now : Time.Time }
init : ( Model, Cmd Msg )
init =
( Model 0, Task.perform never TrackTime Time.now )
-- UPDATE
type Msg
= TrackTime Time.Time
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
TrackTime time ->
( { model | now = time }, Cmd.none )
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
-- VIEW
view : Model -> Html Msg
view model =
h1 [] [ text <| " now: " ++ toString model.now ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment