Class: Timer
Overview
Ruby license. Copyright ©2004 Joel VanderWerf. Contact [email protected].
A lightweight, non-drifting, self-correcting timer. Average error is bounded as long as, on average, there is enough time to complete the work done, and the timer is checked often enough. It is lightweight in the sense that no threads are created. Can be used either as an internal iterator (Timer.every) or as an external iterator (Timer.new).
Simple usage:
require ‘timer’
Timer.every(0.1, 0.5) { |elapsed| puts elapsed }
timer = Timer.new(0.1) 5.times do
puts timer.elapsed
timer.wait
end
Instance Attribute Summary collapse
-
#end_time ⇒ Object
readonly
Returns the value of attribute end_time.
-
#period ⇒ Object
readonly
Returns the value of attribute period.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
Class Method Summary collapse
-
.every(period, expire = nil) ⇒ Object
Yields to the supplied block every
period
seconds.
Instance Method Summary collapse
-
#elapsed ⇒ Object
Time on timer since instantiation or last #restart.
-
#if_ready ⇒ Object
Yield to the block if no time remains in cycle.
-
#initialize(period = 1) ⇒ Timer
constructor
Make a Timer that can be checked when needed, using #wait or #if_ready.
-
#restart ⇒ Object
Call this to restart the timer after a period of inactivity (e.g., the user hits the pause button, and then hits the go button).
- #running? ⇒ Boolean
- #start ⇒ Object
- #stop ⇒ Object
- #stopped? ⇒ Boolean
- #total_time ⇒ Object
-
#wait ⇒ Object
Wait for the next cycle, if time remains in the current cycle.
Constructor Details
#initialize(period = 1) ⇒ Timer
Make a Timer that can be checked when needed, using #wait or #if_ready. The advantage over Timer.every is that the timer can be checked on separate passes through a loop.
41 42 43 44 45 46 47 |
# File 'lib/carat-dev/timer/timer.rb', line 41 def initialize @start_time = nil @end_time = nil @total_time = 0 @runnning = nil start end |
Instance Attribute Details
#end_time ⇒ Object (readonly)
Returns the value of attribute end_time.
52 53 54 |
# File 'lib/carat/timer.rb', line 52 def end_time @end_time end |
#period ⇒ Object (readonly)
Returns the value of attribute period.
46 47 48 |
# File 'lib/carat-dev/timer/timer.rb', line 46 def period @period end |
#start_time ⇒ Object (readonly)
Returns the value of attribute start_time.
51 52 53 |
# File 'lib/carat/timer.rb', line 51 def start_time @start_time end |
Class Method Details
.every(period, expire = nil) ⇒ Object
Yields to the supplied block every period
seconds. The value yielded is the total elapsed time (an instance of Time
). If expire
is given, then #every returns after that amount of elapsed time.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/carat-dev/timer/timer.rb', line 26 def Timer.every(period, expire = nil) target = time_start = Time.now loop do elapsed = Time.now - time_start break if expire and elapsed > expire yield elapsed target += period error = target - Time.now sleep error if error > 0 end end |
Instance Method Details
#elapsed ⇒ Object
Time on timer since instantiation or last #restart.
55 56 57 |
# File 'lib/carat-dev/timer/timer.rb', line 55 def elapsed Time.now - @time_start end |
#if_ready ⇒ Object
Yield to the block if no time remains in cycle. Otherwise, return immediately to caller
70 71 72 73 74 75 76 77 |
# File 'lib/carat-dev/timer/timer.rb', line 70 def if_ready error = @target + @period - Time.now if error <= 0 @target += @period elapsed = Time.now - @time_start yield elapsed end end |
#restart ⇒ Object
Call this to restart the timer after a period of inactivity (e.g., the user hits the pause button, and then hits the go button).
50 51 52 |
# File 'lib/carat-dev/timer/timer.rb', line 50 def restart @target = @time_start = Time.now end |
#running? ⇒ Boolean
73 74 75 |
# File 'lib/carat/timer.rb', line 73 def running? return @running end |
#start ⇒ Object
62 63 64 65 |
# File 'lib/carat/timer.rb', line 62 def start @start_time = Time.now @running = true end |
#stop ⇒ Object
67 68 69 70 71 |
# File 'lib/carat/timer.rb', line 67 def stop @end_time = Time.now @total_time += @end_time - @start_time @running = false end |
#stopped? ⇒ Boolean
77 78 79 |
# File 'lib/carat/timer.rb', line 77 def stopped? return !@running end |