Developer Resources

Timer

Overview

The Timer class represents a timer for firing events at a specified interval.

Constructor

Timer()

Events

Timer.tick
Fired each time a timer interval has passed.

Methods

Timer.getInterval
Gets the timer event interval.
Timer.isRunning
Returns true if the timer is running and false otherwise.
Timer.setInterval
Set the timer event interval.
Timer.start
Starts the timer
Timer.stop
Stops the timer

Example

// create a new instance of our timer class
// and start the application event loop
var t = new MyTimer;
Application.run();


// define our timer class
class MyTimer
{
    function MyTimer()
    {
        // initialize a member variable to track the
        // total amount of time that has elapsed once
        // the timer is started
        m_time_total = 0;

        // initialize the interval in milliseconds at 
        // which to fire the timer event
        m_time_interval = 100;

        // create a new timer object, set it to fire the
        // tick event at the specified interval, and connect
        // the tick event to the onTick member function
        m_timer = new Timer;
        m_timer.setInterval(m_time_interval);
        m_timer.tick.connect(this, onTick);
        
        // start the timer
        m_timer.start();
    }
    
    function onTick()
    {
        // ntoe: the onTick member function will be called
        // everytime the specified interval has elapsed,
        // which in this case is 100 milliseconds
    
        // track the total time since the event
        // started firing
        m_time_total += m_time_interval;
        
        // if 1 second has elapsed, stop the timer
        // and issue an alert
        if (m_time_total >= 1000)
        {
            m_timer.stop();
            alert("1 second has elapsed");
        }
    }

    // member variables
    var m_time_total;
    var m_time_interval;
    var m_timer;
}

Timer.getInterval

function Timer.getInterval() : Integer

Returns

The time interval in milliseconds at which the the timer tick event is to be fired.

Description

This function returns the time interval in milliseconds at which the timer tick event is to be fired.

Timer.isRunning

function Timer.isRunning() : Boolean

Returns

True if the timer is running and false otherwise.

Description

This function indicates whether or not the timer is currently running, and therefore, firing timer events. Returns true if the timer is running and false otherwise.

Timer.setInterval

function Timer.setInterval(interval : Integer)

Arguments

interval
The time interval in milliseconds at which the the timer tick event is to be fired.

Description

This function sets the time interval in milliseconds at which the timer tick event is to be fired.

Timer.start

function Timer.start()

Description

This function starts the timer, so that the tick event is fired at a periodic interval.

Timer.stop

function Timer.stop()

Description

This function stops the timer, so that the tick event stops firing.