Crystal 中是否有定时器或间隔函数?
我查看了定时器、间隔和 Time 类的文档,但没有找到任何相关内容。
类似于 JavaScript 中的 setInterval()
或 setTimeout()
之类的东西吗?
Crystal 中是否有定时器或间隔函数?
我查看了定时器、间隔和 Time 类的文档,但没有找到任何相关内容。
类似于 JavaScript 中的 setInterval()
或 setTimeout()
之类的东西吗?
目前没有确保精确定时的间隔方法,但如果不需要精确的时间间隔,只需要一个近似值,那么很容易做到:
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.
delay
实际上是一个 future 对象的实现,因此它可以返回一个值并且可以被取消。如果您不需要这些功能,应该使用 spawn
+ sleep
。 - Johannes Müllerdelay
功能:https://crystal-lang.org/2020/06/09/crystal-0.35.0-released.html(背景信息:https://github.com/crystal-lang/crystal/pull/9093) - Philipp Claßenhttps://github.com/hugoabonizio/schedule.cr
require "schedule"
# Print "Hello!" each 2 seconds
Schedule.every(2.seconds) do
puts "Hello!"
end
sleep