Class: Livecode::Timer
- Inherits:
-
Object
- Object
- Livecode::Timer
- Defined in:
- lib/livecode/timer.rb
Overview
Proc objects are also supported:
timer_proc = proc{puts "I'm a proc"}
timer2 = Timer.new(100, timer_proc)
Timer will try to compensate for the run time of your code in order to stay in sync.
Instance Attribute Summary collapse
-
#proc ⇒ Object
Returns the value of attribute proc.
-
#time ⇒ Object
Returns the value of attribute time.
Instance Method Summary collapse
-
#call ⇒ Object
Call the block.
-
#initialize(time, proc = nil, &block) ⇒ Timer
constructor
A new instance of Timer.
-
#start ⇒ Object
(also: #run)
Start the timer.
-
#stop ⇒ Object
(also: #clear, #end)
Stop the timer.
Constructor Details
#initialize(time, proc = nil, &block) ⇒ Timer
Returns a new instance of Timer.
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/livecode/timer.rb', line 22 def initialize(time, proc=nil, &block) @time = time || 1 if proc @proc = proc elsif block_given? @proc = block end @tread = nil start end |
Instance Attribute Details
#proc ⇒ Object
Returns the value of attribute proc.
20 21 22 |
# File 'lib/livecode/timer.rb', line 20 def proc @proc end |
#time ⇒ Object
Returns the value of attribute time.
20 21 22 |
# File 'lib/livecode/timer.rb', line 20 def time @time end |
Instance Method Details
#call ⇒ Object
Call the block
34 35 36 37 38 39 |
# File 'lib/livecode/timer.rb', line 34 def call Silenceable.apply(@proc) if @proc if @proc && !@proc.silenced? @proc.call end end |
#start ⇒ Object Also known as: run
Start the timer
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/livecode/timer.rb', line 42 def start stop timer = self @thread = Thread.new do next_time = Time.now while true timer.call next_time += (timer.time.to_f / 1000) sleep_time = next_time - Time.now sleep(sleep_time) if sleep_time > 0 end end end |
#stop ⇒ Object Also known as: clear, end
Stop the timer.
58 59 60 61 62 63 |
# File 'lib/livecode/timer.rb', line 58 def stop if @thread @thread.exit @thread = nil end end |