Class: EventMachine::PeriodicTimer

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(interval, callback = nil, &block) ⇒ PeriodicTimer

Create a new periodic timer that executes every interval seconds



31
32
33
34
35
# File 'lib/em/timers.rb', line 31

def initialize interval, callback=nil, &block
  @interval = interval
  @code = callback || block
  schedule
end

Instance Attribute Details

#intervalObject

Fire the timer every interval seconds



43
44
45
# File 'lib/em/timers.rb', line 43

def interval
  @interval
end

Instance Method Details

#cancelObject

Cancel the periodic timer



38
39
40
# File 'lib/em/timers.rb', line 38

def cancel
  @cancelled = true
end

#fireObject

:nodoc:



48
49
50
51
52
53
# File 'lib/em/timers.rb', line 48

def fire # :nodoc:
  unless @cancelled
    @code.call
    schedule
  end
end

#scheduleObject

:nodoc:



45
46
47
# File 'lib/em/timers.rb', line 45

def schedule # :nodoc:
  EventMachine::add_timer @interval, proc {self.fire}
end