Class: Timer
- Inherits:
-
Object
- Object
- Timer
- Defined in:
- lib/tabscroll/timer.rb
Overview
A simple timer that counts in seconds.
Usage:
timer = Timer.new
timer.start
...
if timer.delta > 0.5 # half a second
...
Instance Method Summary collapse
-
#delta ⇒ Object
Returns the current delta in seconds (float).
-
#initialize ⇒ Timer
constructor
Creates the timer, doing nothing else.
- #pause ⇒ Object
- #paused? ⇒ Boolean
- #restart ⇒ Object
- #running? ⇒ Boolean
-
#start ⇒ Object
Starts counting.
-
#stop ⇒ Object
Stops counting.
-
#to_s ⇒ Object
Converts the timer’s delta to a formatted string.
- #unpause ⇒ Object
Constructor Details
#initialize ⇒ Timer
Creates the timer, doing nothing else.
13 14 15 16 |
# File 'lib/tabscroll/timer.rb', line 13 def initialize @is_running = false @is_paused = false end |
Instance Method Details
#delta ⇒ Object
Returns the current delta in seconds (float).
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/tabscroll/timer.rb', line 68 def delta if @is_running return (Time.now.to_f - @start_time.to_f) end return @paused_time.to_f if @is_paused return @start_time if @start_time == 0 # Something's wrong return (@stop_time.to_f - @start_time.to_f) end |
#pause ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/tabscroll/timer.rb', line 43 def pause return if not @is_running or @is_paused @paused_time = (Time.now - @start_time) @is_running = false @is_paused = true end |
#paused? ⇒ Boolean
63 64 65 |
# File 'lib/tabscroll/timer.rb', line 63 def paused? @is_paused end |
#restart ⇒ Object
38 39 40 41 |
# File 'lib/tabscroll/timer.rb', line 38 def restart self.stop self.start end |
#running? ⇒ Boolean
59 60 61 |
# File 'lib/tabscroll/timer.rb', line 59 def running? @is_running end |
#start ⇒ Object
Starts counting.
19 20 21 22 23 24 25 26 27 |
# File 'lib/tabscroll/timer.rb', line 19 def start return if @is_running @start_time = Time.now @stop_time = 0.0 @paused_time = 0.0 @is_running = true @is_paused = false end |
#stop ⇒ Object
Stops counting.
30 31 32 33 34 35 36 |
# File 'lib/tabscroll/timer.rb', line 30 def stop return if not @is_running @stop_time = Time.now @is_running = false @is_paused = false end |
#to_s ⇒ Object
Converts the timer’s delta to a formatted string.
81 82 83 84 85 86 87 |
# File 'lib/tabscroll/timer.rb', line 81 def to_s min = (self.delta / 60).to_i sec = (self.delta).to_i msec = (self.delta * 100).to_i "#{min}:#{sec}:#{msec}" end |
#unpause ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/tabscroll/timer.rb', line 51 def unpause return if not @is_paused or @is_running @start_time = (Time.now - @paused_time) @is_running = true @is_paused = false end |