123456789_123456789_123456789_123456789_123456789_

Class: EventMachine::PeriodicTimer

Relationships & Source Files
Inherits: Object
Defined in: lib/em/timers.rb

Overview

Creates a periodic timer

Examples:

n = 0
timer = EventMachine::PeriodicTimer.new(5) do
  puts "the time is #{Time.now}"
  timer.cancel if (n+=1) > 5
end

Class Method Summary

Instance Attribute Summary

  • #interval rw

    Fire the timer every interval seconds.

Instance Method Summary

  • #cancel

    Cancel the periodic timer.

  • #fire Internal use only Internal use only
  • #schedule Internal use only Internal use only

Constructor Details

.new(interval, callback = nil, &block) ⇒ PeriodicTimer

Create a new periodic timer that executes every interval seconds

[ GitHub ]

  
# File 'lib/em/timers.rb', line 32

def initialize interval, callback=nil, &block
  @interval = interval
  @code = callback || block
  @cancelled = false
  @work = method(:fire)
  schedule
end

Instance Attribute Details

#interval (rw)

Fire the timer every interval seconds

[ GitHub ]

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

attr_accessor :interval

Instance Method Details

#cancel

Cancel the periodic timer

[ GitHub ]

  
# File 'lib/em/timers.rb', line 41

def cancel
  @cancelled = true
end

#fire

This method is for internal use only.
[ GitHub ]

  
# File 'lib/em/timers.rb', line 54

def fire
  unless @cancelled
    @code.call
    schedule
  end
end

#schedule

This method is for internal use only.
[ GitHub ]

  
# File 'lib/em/timers.rb', line 49

def schedule
  EventMachine::add_timer @interval, @work
end