Elixir Sleep / Wait for 1 Second
Asked Answered
B

2

87

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.

Bellyful answered 9/5, 2016 at 1:17 Comment(1)
Also in Elixir 1.3 you can use Process.sleep instead of calling erlang.Phalanx
N
113

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

Narayan answered 9/5, 2016 at 1:22 Comment(1)
There are useful helpers in the timer module to specify time intervals. For example: :timer.sleep(:timer.seconds(1))Zashin
B
90

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

Process.sleep(1000)

The argument is in milliseconds.

Bicollateral answered 25/3, 2018 at 0:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.