Automatically redirect user after a few seconds

After a user logs in, I want to show a brief message, then redirect. It seems I can send an update within an update by doing this:

LogIn (Ok newUser) ->
    ( { model | user = newUser, showPopup = True }
    , Task.perform ShowDashboard )

But how can I delay that update? I saw some code which made me belief I could write:

LogIn (Ok newUser) ->
    ( { model | user = newUser, showPopup = True }
    , Process.sleep 5 |> Task.perform ShowDashboard )

I don’t yet know what this means (still learning Elm), but this doesn’t compile.

In JavaScript this would be done with using setTimeout(). The closest answer I could google was this: but I couldn’t get any of the code described there to compile. Obviously my inability yet to read and write Elm highly contributes to that, so when I see things like “! [ ]” or “|>” I’m getting lost a bit :slight_smile.

1 Like


The main trick here is to first sleep, and then get the time. In code

Process.sleep 2000 |> Task.andThen (\_ ->

note that the time is in miliseconds

I’ve made a full example here as an ellie.

Hope that helps


This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.