Class: Clearsight::Timer
- Inherits:
-
Object
- Object
- Clearsight::Timer
- Defined in:
- lib/clearsight.rb
Instance Method Summary collapse
-
#initialize(interval, &handler) ⇒ Timer
constructor
A new instance of Timer.
- #stop ⇒ Object
Constructor Details
#initialize(interval, &handler) ⇒ Timer
Returns a new instance of Timer.
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/clearsight.rb', line 8 def initialize(interval, &handler) @run = true @semaphore = Mutex.new @th = Thread.new do t = Time.now while run? t += interval (sleep(t - Time.now) rescue nil) and handler.call rescue nil end end end |
Instance Method Details
#stop ⇒ Object
21 22 23 24 25 26 |
# File 'lib/clearsight.rb', line 21 def stop @semaphore.synchronize do @run = false end @th.join end |