Elixir Sleep / Wait for 1 Second

ElixirElixir Iex

Elixir Problem Overview


How to sleep / wait for one second?

Best I could find was something like this (in iex):

IO.puts "foo" ; :timer.sleep(1); IO.puts "bar"

But both of my puts happen with no delay.

Elixir Solutions


Solution 1 - Elixir

Timer uses milliseconds not seconds, update to:

IO.puts "foo" ; :timer.sleep(1000); IO.puts "bar"

Documentation of :timer in Erlang's doc:

> Suspends the process calling this function for Time amount of > milliseconds and then returns ok, or suspend the process forever if > Time is the atom infinity. Naturally, this function does not return > immediately.

http://erlang.org/doc/man/timer.html#sleep-1

Solution 2 - Elixir

Since Elixir 1.3 you can use Process.sleep/1:

Process.sleep(1000)

The argument is in milliseconds.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnewUserNameHereView Question on Stackoverflow
Solution 1 - ElixirJeremie GesView Answer on Stackoverflow
Solution 2 - ElixirAlexandre L TellesView Answer on Stackoverflow