84

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.

newUserNameHere
  • 17,348
  • 18
  • 49
  • 79

2 Answers2

112

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

Jeremie Ges
  • 2,747
  • 3
  • 22
  • 37
  • 33
    There are useful helpers in the timer module to specify time intervals. For example: `:timer.sleep(:timer.seconds(1))` – Paweł Obrok May 09 '16 at 08:17
87

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

Process.sleep(1000)

The argument is in milliseconds.

Marius Butuc
  • 17,781
  • 22
  • 77
  • 111
Alexandre L Telles
  • 3,375
  • 29
  • 26