Crystal Lang中的定时器/间隔

dki*_*mot 5 crystal-lang

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

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

喜欢的东西setInterval()或者setTimeout()从JavaScript?

Jon*_*Haß 6

对于超时,有延迟.请注意,此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.
Run Code Online (Sandbox Code Playgroud)