Lua:Class:Timer
The Timer class represents a timer component.
A Timer inherits from Component and Object. It can repeatedly call an OnTimer function after a given interval, or it can be created as a one-shot timer that executes a callback once and then destroys itself.
Contents
Inheritance
| Class | Inherits From | Description |
|---|---|---|
| Timer | Component | A timer component that can execute code after a delay or repeatedly at an interval. |
| Component | Object | Base class for components. |
Creation
Creates a Timer object.
The first form creates a one-shot timer that waits for the given delay, executes the given function, and then destroys itself. Do not use the timer object after it has fired.
The second form creates a persistent timer object. If enabled is omitted, it will be enabled by default and starts running as soon as an OnTimer event has been assigned. The owner may be nil, but in that case you are responsible for destroying the timer manually.
Function Parameters
One-Shot Timer
| Parameter | Type | Description |
|---|---|---|
| delay | Integer | The delay in milliseconds before the callback is executed. |
| function | Function | The function to execute when the timer fires. |
| ... | Any (optional) | Additional arguments that are passed to the callback function. |
Persistent Timer
| Parameter | Type | Description |
|---|---|---|
| owner | Component or nil (optional) | The owner of the timer. If nil or omitted, the timer must be destroyed manually. |
| enabled | Boolean (optional) | If true, the timer is enabled. If omitted, the timer is enabled by default. |
Returns
Timer — The created Timer object.
Properties
| Property | Type | Description |
|---|---|---|
| Interval | Integer | The number of milliseconds between timer executions. 1000 equals one second. |
| Enabled | Boolean | If true, the timer is active. If false, the timer is paused. |
| OnTimer | Function | The function to call when the timer triggers. The timer object is passed as the parameter. |
Methods
| Method | Return Type | Description |
|---|---|---|
| getInterval() | Integer | Returns the timer interval in milliseconds. |
| setInterval(interval) | void | Sets how often the timer should trigger, in milliseconds. |
| getOnTimer() | Function | Returns the current OnTimer function. |
| setOnTimer(function(timer)) | void | Sets the function that is called when the timer triggers. |
| getEnabled() | Boolean | Returns whether the timer is currently enabled. |
| setEnabled(boolean) | void | Enables or disables the timer. |
Examples
1 local timer = createTimer(nil, false)
2
3 timer.Interval = 1000
4 timer.OnTimer = function(timer)
5 print("Tick")
6 end
7
8 timer.Enabled = true
1 local timer = createTimer()
2 timer.Interval = 1000
3
4 local count = 0
5
6 timer.OnTimer = function(timer)
7 count = count + 1
8 print("Count: " .. tostring(count))
9
10 if count >= 5 then
11 timer.Enabled = false
12 timer.destroy()
13 end
14 end
15 </pre>
16
17 <syntaxhighlight lang="lua" line>
18 createTimer(2000, function()
19 print("This runs once after 2 seconds")
20 end)
1 createTimer(1500, function(name, value)
2 print(name .. ": " .. tostring(value))
3 end, "Health", 100)
1 local form = createForm()
2 form.Caption = "Timer Example"
3
4 local timer = createTimer(form)
5 timer.Interval = 500
6 timer.OnTimer = function(timer)
7 form.Caption = os.date("%H:%M:%S")
8 end
9
10 form.show()