Created
October 24, 2018 02:52
-
-
Save christiankakesa/247218128b63d21830528c9b93c46955 to your computer and use it in GitHub Desktop.
Example of simple ticker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Ticker | |
getter tick | |
@tick = Channel(Bool).new | |
def initialize(tick_s : Int32) | |
spawn do | |
loop do | |
sleep tick_s | |
@tick.send true | |
end | |
end | |
end | |
end | |
ticker = Ticker.new(2) # 2 seconds | |
spawn do | |
loop do | |
select | |
when ticker.tick.receive | |
puts "#{Time.now}" | |
end | |
end | |
end | |
sleep 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment