Elixir睡眠/等待1秒

new*_*ere 54 elixir iex

如何睡觉/等待一秒钟?

我能找到的最好的东西是这样的(在iex中):

IO.puts "foo" ; :timer.sleep(1); IO.puts "bar"
Run Code Online (Sandbox Code Playgroud)

但是我的两个看法都没有延迟.

Jer*_*Ges 76

Timer使用毫秒而非秒,更新为:

IO.puts "foo" ; :timer.sleep(1000); IO.puts "bar"
Run Code Online (Sandbox Code Playgroud)

文档:Erlang文档中的计时器:

暂停调用此函数的过程的Time of milliseconds,然后返回ok,或者如果Time是原子无穷大,则永久暂停该过程.当然,此功能不会立即返回.

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

  • 定时器模块中有一些有用的助手来指定时间间隔.例如:`:timer.sleep(:timer.seconds(1))` (27认同)

Ale*_*les 34

从Elixir 1.3开始,您可以使用Process.sleep/1:

Process.sleep(1000)
Run Code Online (Sandbox Code Playgroud)

参数以毫秒为单位.