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 Time.now )

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 Time.now )

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: https://stackoverflow.com/questions/40599512/how-to-achieve-behavior-of-settimeout-in-elm 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

Hi,

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

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

note that the time is in miliseconds

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

Hope that helps

5 Likes

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