wxRuby Documentation Home


The Timer class allows you to execute code at specified intervals. Its precision is platform-dependent, but in general will not be better than 1ms nor worse than 1s.

There are several different ways to use this class:

The most convenient way is to use the Timer.every class method, which accepts a Integer to specify the millisecond interval at which the Timer should fire, and a block to specify what should happen at that interval. For example, to print a message to STDOUT every 1/2 second:

Wx::Timer.every(500) do
  puts "Tick" 

Secondly you may use the event handling mechanism to direct events to any object which inherits from EvtHandler. To do this use the longer constructor or set_owner. Then use the evt_timer method to connect it to the event handler which will receive TimerEvent notifications.

  1. assuming ‘self’ is some Window or Wx::App timer = Wx::Timer.new(self) evt_timer(timer.id) { puts “Timer ticked” } timer.start(100)

Lastly, you may derive a new class from Timer and define your own notify instance method in that to perform the required action. For example:

class MyTimer < Wx::Timer
  def notify
    puts "Timer ticked" 
timer = MyTimer.new
timer.start(100) # every 1/10 second

Unless you use Timer.every, you must start the timer with start after constructing it before it actually starts sending notifications. It can be stopped later with stop

Derived from

EvtHandler, Object

See also

::StartTimer, ::GetElapsedTime, StopWatch



Timer.new(EvtHandler owner,  Integer id = -1)

Creates a timer and associates it with owner. Please see set_owner for the description of parameters.


Timer.after(Integer interval) { ... }

Carries out the action specified in the passed block after interval milliseconds have passed. Note that the action will happen only once; if you want a repeating action, use Timer.every

If you wish to interrupt the timed event before it may have happened, capture the return value which is the newly created timer for this action, and call stop on it.


Timer.every(Integer interval) { ... }

Repeats the action specified in the passed block every interval milliseconds. The timer is owned by the application object and will fire as long as the application is running.

The method returns the new timer; if you may wish to stop the timer before the application finishes, you should capture this return value so you can call stop on it later.


Integer get_id()

Returns the id of the timer.


Integer get_interval()

Returns the current interval for the timer (in milliseconds).


Boolean is_one_shot()

Returns true if the timer is one shot, i.e. if it will stop after firing the first notification automatically.


Boolean is_running()

Returns true if the timer is running, false if it is stopped.



This member should be overridden by the user if the default constructor was used and set_owner wasn’t called.

Perform whatever action which is to be taken periodically here.


set_owner(EvtHandler owner,  Integer id = -1)

Associates the timer with the given owner\/ object. When the timer is running, the owner will receive timer events with id equal to id\/ specified here.


Boolean start(Integer milliseconds = -1,  Boolean oneShot = false)

(Re)starts the timer. If milliseconds\/ parameter is -1 (value by default), the previous value is used. Returns false if the timer could not be started, true otherwise (in MS Windows timers are a limited resource).

If oneShot\/ is false (the default), the Notify function will be called repeatedly until the timer is stopped. If true, it will be called only once and the timer will stop automatically. To make your code more readable you may also use the following symbolic constants:

TIMER_CONTINUOUS Start a normal, continuously running, timer
TIMER_ONE_SHOT Start a one shot timer

If the timer was already running, it will be stopped by this method before restarting it.



Stops the timer.

[This page automatically generated from the Textile source at Wed Sep 09 02:21:27 +0100 2009]