Crystal Lang中的定时器/间隔

时间:2018-03-19 09:02:17

标签: crystal-lang

Crystal中是否有定时器或间隔功能?

我检查了文档中的计时器,间隔和Time类,但没有看到任何内容。

来自JavaScript的setInterval()setTimeout()

2 个答案:

答案 0 :(得分:5)

对于超时,delay。请注意,此API尚未最终确定,可能会在将来的版本中进行更改,甚至可能会再次暂时删除。

对于间隔,目前没有任何东西可以保证准确的时间,但如果不关心并且近似间隔就足够了,那就像

一样简单。
spawn do
  loop do
    sleep INTERVAL
    do_regular_work
  end
end

sleep # Or some other workload, when the main fiber quits so will the program and thus all other fibers.

答案 1 :(得分:0)

https://github.com/hugoabonizio/schedule.cr

require "schedule"

# Print "Hello!" each 2 seconds
Schedule.every(2.seconds) do
  puts "Hello!"
end

sleep
相关问题