Let's take a closer look at the object:
The Time object
The constructor
To create the Timer object the constructor has to be used taking 2 arguments:
var my_timer = new Timer(argument1, argument2);
The arguments are as follows:
-
-
a float number representing the time interval in milliseconds at which the OnTick even will be raised
- the second parameter can hold 2 different values:
-
a boolean value (true or false) representing whether the timer should execute only once and keep executing until stopped
-
an integer value indicating the number of executions (raises of onTick events)
//the timer will run just once, so the interval makes no sense here var my_timer = new Timer(100, false); //the timer will repeat 5 times with 1000 milliseconds intervals
var my_timer = new Timer(1000, 5); //the timer will repeat until stopped with 500 milliseconds intervals
var my_timer = new Timer(500, true);
The methods
Dispose
The Dispose method stops the timer and disposes the object. It is always a convenient rule to clean the objects from memory once you done with them. Even though there is a garbage collector implemented that will clean after you, some objects hold some resources that the collector is not able to cope with. The Timer object holds a handle to the system timer, so the collector will not help here. Therefore, you should always dispose the instance of the Timer object to remove from the memory.
//the timer will be stopped and disposed from the memory my_timer.dispose();
Start
This method basically starts the timer. There is an optional Integer parameter representing a delayed start in milliseconds.
//the timer will start rightaway my_timer.start(); //the timer will start after 500 milliseconds my_timer.start(500);
Stop
Stops the timer making it valid and persistent in memory for a later use. Calling the Stop method will not remove the instance from the memory. To destruct the object totally and to remove it from memory, the dispose() method has to be used.
//the timer will be stopped but ready to be reused my_timer.stop();
The onTick event
The onTick event is raised according to the timer intervals set in the constructor. This event should contain all the code you want to execute then the timer ticks.
my_timer.onTick = function(sender) { //your code you want to execute goes here }
Working with the Timer
In following demo, you can see the use of timers in a practical example. The only code that is executed when the timers tick is some printout into the console. An empty game project was created to hold the timer objects inside, so we can track the execution in the console.
//timer will tick only once
var once = new Timer(100, false); // interval make no sense here
once.start(500); //delayed start by 500 millisecond
once.onTick = function(sender) {
console << "timer once tick\n";
} //timer will tick 5 times with 500 milliseconds between the ticks
var five = new Timer(500, 5);
five.start(); //starting right away
five.onTick = function(sender) {
console << "timer five tick\n";
}
In the following console output, we can see that the second timer ticked before the first one. This is due to the delay that we have specified in the start method.

Summary
Every time you need to execute some code in specific time intervals, you have to use the Timer object. Once the instance of the object is created, use only start and stop methods to operate with it. Once you don't need it anymore, do not forget to call the dispose method to remove the instance from the memory.
|