Search code examples
elmpause

How to pause for a measured time in Elm?


I am attempting to create a simple web page using Elm which involves displaying an image for a measured amount of time, such as a second or a millisecond.

Is there a way to "pause" in Elm? Then I could display, pause, and then remove the image to achieve the effect.

I noticed the clock example but that appears to update every time the machine's clock triggers a new second, whereas I am looking for something that can pause to update a set time (such as a fraction of a second) after the starting time.


Solution

  • You can create a new Msg value which triggers url (of the image) changes. And create a subscription via Timer.every. You can add a flag in your model to indicate whether keep updating the image or not, if that's what you want.

    Example:

    subscriptions : Model -> Sub Msg
    subscriptions model =
        if model.keepUpdating then
            Time.every (3 * second) (\x -> UpdateImage)
        else
            Sub.none
    
    
    update : Msg -> Model -> ( Model, Cmd Msg )
    update msg model =
        case msg of
            UpdateImage ->
                ( { model | image_url = "http://to_new_image/..." }, Cmd.none )