Timer in elm

313 views
Skip to first unread message

Juan Martin Buireo

unread,
Jun 18, 2016, 7:07:10 PM6/18/16
to Elm Discuss
Hi, I am currently developing an elm-game in version 0.17

I read the guide and the example about the time (http://guide.elm-lang.org/architecture/effects/time.html). I understood the example but now I need to use that to make something similar.

What i need to do is a chronometer. The chronometer starts in zero and as time passes, seconds and minutes increase. This will be printed as a string. But what I notice about the example of the Time is that no matter that I set the time initally to zero, when it runs it gives me the actual time. Another problem that I have is that when i do the 

toString (inMinutes time) this prints me something like 24438186.788516667. I want the minutes as [0, 59].

Thanks

Dan P

unread,
Jun 18, 2016, 7:56:35 PM6/18/16
to Elm Discuss
Add a field called startTime : Time to your model, which tracks when the timer was started. Taking the difference between the current time and startTime gives you the first quantity you wanted. To get the second quantity, write (t % 60) // 1. The (//) operation rounds it down,  and the (%) operation does the rest.

Juan Martin Buireo

unread,
Jun 18, 2016, 8:00:31 PM6/18/16
to Elm Discuss
Thanks, that was just what I was thinking. The problem is that I dont know how to set the start time without a subscription.

Dan P

unread,
Jun 18, 2016, 8:22:23 PM6/18/16
to Elm Discuss
Assuming there's a field in your model with the current time, I'd just make a message SetStartTime which makes the update function return { model | startTime = currentTime }.

Juan Martin Buireo

unread,
Jun 18, 2016, 8:51:34 PM6/18/16
to Elm Discuss
Thanks again for the response. The problem is the following:

My model is: 
type alias Model = { cards : List IndexedCard, startTime : Time, currentTime : Time }

And I have a subscription where I do:
subscriptions : Model -> Sub Msg
subscriptions model = Time.every Time.second Tick

So with this, I have to questions, in the init function, how should I initialize startTime and currentTime? That's what I don't know 

Juan Martin Buireo

unread,
Jun 18, 2016, 8:53:53 PM6/18/16
to Elm Discuss
I forgot to put the update function:

update msg ({cards, startTime, currentTime} as model) =
    case msg of
        FlipCard id message -> ({ model | cards = checkCards (updateCards id message cards) }, Cmd.none)
        Restart -> init
        Tick newTime -> ({ model | currentTime = newTime }, Cmd.none) 

Dan P

unread,
Jun 18, 2016, 9:36:41 PM6/18/16
to Elm Discuss
I think you'll have to initialize your start time to 0, and change your update code to the following:

Tick newTime -> if (.model startTime) == 0 then ({model | startTime = newTime, currentTime = newTime})
 
else ({ model | currentTime = newTime }, Cmd.none)

Johann Kaup

unread,
Jun 19, 2016, 1:08:32 PM6/19/16
to Elm Discuss
To prevent having to wait for the first time tick after initialization you can also add it as a command to your init function.

init : ( Model, Cmd Msg )
init
=
   
( { cards = ...
     
, startTime = 0
     
, currentTime = 0
     
}
   
, Time.now |> Task.perform (always (StartTick 0)) StartTick
   
)


And of course handle the StartTick event in your update function.
Reply all
Reply to author
Forward
0 new messages