Difference between revisions of "Lua:Class:Timer"
(typo fix) |
m |
||
| (23 intermediate revisions by 6 users not shown) | |||
| Line 1: | Line 1: | ||
| − | Timer | + | [[Category:Lua]] |
| − | + | {{Class|'''class''' Timer ''':''' Component}} | |
| + | 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. | |
| − | |||
| − | |||
| + | ===Inheritance=== | ||
| + | {|width="85%" cellpadding="10%" cellspacing="0" border="0" | ||
| + | !align="left"|Class | ||
| + | !align="left"|Inherits From | ||
| + | !style="width: 80%;background-color:white;" align="left"|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=== | |
| − | + | {{CodeBox|'''function''' createTimer(''delay'', ''function'', ''...'') ''':''' Timer}} | |
| + | {{CodeBox|'''function''' createTimer(''owner'', ''enabled'') ''':''' Timer}} | ||
| + | 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==== | |
| + | {|width="85%" cellpadding="10%" cellspacing="0" border="0" | ||
| + | !align="left"|Parameter | ||
| + | !align="left"|Type | ||
| + | !style="width: 80%;background-color:white;" align="left"|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==== | ||
| + | {|width="85%" cellpadding="10%" cellspacing="0" border="0" | ||
| + | !align="left"|Parameter | ||
| + | !align="left"|Type | ||
| + | !style="width: 80%;background-color:white;" align="left"|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=== | |
| + | {|width="85%" cellpadding="10%" cellspacing="0" border="0" | ||
| + | !align="left"|Property | ||
| + | !align="left"|Type | ||
| + | !style="width: 80%;background-color:white;" align="left"|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. | ||
| + | |} | ||
| − | function | + | ===Methods=== |
| − | print " | + | {|width="85%" cellpadding="10%" cellspacing="0" border="0" |
| + | !align="left"|Method | ||
| + | !align="left"|Return Type | ||
| + | !style="width: 80%;background-color:white;" align="left"|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=== | ||
| + | <pre> | ||
| + | local timer = createTimer(nil, false) | ||
| + | |||
| + | timer.Interval = 1000 | ||
| + | timer.OnTimer = function(timer) | ||
| + | print("Tick") | ||
| + | end | ||
| + | |||
| + | timer.Enabled = true | ||
| + | </pre> | ||
| + | |||
| + | <pre> | ||
| + | local timer = createTimer() | ||
| + | timer.Interval = 1000 | ||
| + | |||
| + | local count = 0 | ||
| + | |||
| + | timer.OnTimer = function(timer) | ||
| + | count = count + 1 | ||
| + | print("Count: " .. tostring(count)) | ||
| + | |||
| + | if count >= 5 then | ||
| + | timer.Enabled = false | ||
| + | timer.destroy() | ||
end | end | ||
| + | end | ||
| + | </pre> | ||
| + | |||
| + | <pre> | ||
| + | createTimer(2000, function() | ||
| + | print("This runs once after 2 seconds") | ||
| + | end) | ||
| + | </pre> | ||
| + | |||
| + | <pre> | ||
| + | createTimer(1500, function(name, value) | ||
| + | print(name .. ": " .. tostring(value)) | ||
| + | end, "Health", 100) | ||
| + | </pre> | ||
| + | |||
| + | <pre> | ||
| + | local form = createForm() | ||
| + | form.Caption = "Timer Example" | ||
| + | |||
| + | local timer = createTimer(form) | ||
| + | timer.Interval = 500 | ||
| + | timer.OnTimer = function(timer) | ||
| + | form.Caption = os.date("%H:%M:%S") | ||
| + | end | ||
| + | |||
| + | form.show() | ||
| + | </pre> | ||
| − | + | {{LuaSeeAlso}} | |
| − | |||
| − | |||
| − | |||
Latest revision as of 19:21, 23 June 2026
{} Class
class Timer : Component
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[edit]
| 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[edit]
<> Reference
function createTimer(delay, function, ...) : Timer
<> Reference
function createTimer(owner, enabled) : Timer
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[edit]
One-Shot Timer[edit]
| 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[edit]
| 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[edit]
Timer — The created Timer object.
Properties[edit]
| 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[edit]
| 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[edit]
local timer = createTimer(nil, false)
timer.Interval = 1000
timer.OnTimer = function(timer)
print("Tick")
end
timer.Enabled = true
local timer = createTimer()
timer.Interval = 1000
local count = 0
timer.OnTimer = function(timer)
count = count + 1
print("Count: " .. tostring(count))
if count >= 5 then
timer.Enabled = false
timer.destroy()
end
end
createTimer(2000, function()
print("This runs once after 2 seconds")
end)
createTimer(1500, function(name, value) print(name .. ": " .. tostring(value)) end, "Health", 100)
local form = createForm()
form.Caption = "Timer Example"
local timer = createTimer(form)
timer.Interval = 500
timer.OnTimer = function(timer)
form.Caption = os.date("%H:%M:%S")
end
form.show()
See Also[edit]
Main Pages