Class: EventMachine::PeriodicTimer
- Inherits:
-
Object
- Object
- EventMachine::PeriodicTimer
- Defined in:
- lib/em/timers.rb
Overview
Creates a periodic timer
n = 0
timer = EventMachine::PeriodicTimer.new(5) do
puts "the time is #{Time.now}"
timer.cancel if (n+=1) > 5
end
Instance Attribute Summary collapse
-
#interval ⇒ Object
Fire the timer every interval seconds.
Instance Method Summary collapse
-
#cancel ⇒ Object
Cancel the periodic timer.
-
#fire ⇒ Object
:nodoc:.
-
#initialize(interval, callback = nil, &block) ⇒ PeriodicTimer
constructor
Create a new periodic timer that executes every interval seconds.
-
#schedule ⇒ Object
:nodoc:.
Constructor Details
#initialize(interval, callback = nil, &block) ⇒ PeriodicTimer
Create a new periodic timer that executes every interval seconds
31 32 33 34 35 36 37 |
# File 'lib/em/timers.rb', line 31 def initialize interval, callback=nil, &block @interval = interval @code = callback || block @cancelled = false @work = method(:fire) schedule end |
Instance Attribute Details
#interval ⇒ Object
Fire the timer every interval seconds
45 46 47 |
# File 'lib/em/timers.rb', line 45 def interval @interval end |
Instance Method Details
#cancel ⇒ Object
Cancel the periodic timer
40 41 42 |
# File 'lib/em/timers.rb', line 40 def cancel @cancelled = true end |
#fire ⇒ Object
:nodoc:
50 51 52 53 54 55 |
# File 'lib/em/timers.rb', line 50 def fire # :nodoc: unless @cancelled @code.call schedule end end |
#schedule ⇒ Object
:nodoc:
47 48 49 |
# File 'lib/em/timers.rb', line 47 def schedule # :nodoc: EventMachine::add_timer @interval, @work end |