EventMachine更改块内周期定时器的延迟

时间:2013-11-22 09:12:30

标签: ruby timer eventmachine

调用周期性定时器,当块内发生错误时,改变定期定时器延迟。我可以使用定期计时器,还是最好的方法是add_timer?

嗨,我想这样做:

EventMachine.run
    EventMachine.add_periodic_timer 1   
    //read from a input
    //if error set timer to 20s
    //if ok set timer to 1s 
    end
end

怎么样? 感谢

1 个答案:

答案 0 :(得分:1)

自己创建PeriodicTimer个对象,并调用其访问者interval=来设置间隔:

EventMachine.run do
  timer = EventMachine::PeriodicTimer.new(1) do
    puts "Timer fired at #{Time.now}"
  end
  # timer.interval = 1
  # timer.interval = 20
end