Class Timer
Timer class.
Loading this module adds the Timer:update() function to be a part of the dash.update() function, so Timer.update() doesn't have to be called separately if dash.update() is called periodically.
Usage:
local Timer = require("timer")
local function timer_func(text)
print("Timeout:", text)
end
local timer1 = Timer:new(2000, false, timer_func, "Timer 1 timeout")
local timer2 = Timer:new(5000, true, timer_func, "Timer 2 timeout")
timer1:start()
timer2:start()
while true do
dash.update()
end
Functions
| Timer:new (timeout, singleshot, callback, ...) | Create a new timer. |
| Timer:set_callback (callback, ...) | Set callback function to timer. |
| Timer:set_timeout (timeout) | Set timer timeout. |
| Timer:set_singleshot (singleshot) | Set timer singleshot mode. |
| Timer:start ([timeout]) | Start timer. |
| Timer:stop () | Stop timer. |
| Timer.update () | Timer update. |
Functions
- Timer:new (timeout, singleshot, callback, ...)
-
Create a new timer.
Parameters:
- timeout int timeout in milliseconds
- singleshot bool true if timer should only run once
- callback function function which is called when timeout occurs
- ... optional additional parameters to callback function
Returns:
-
Timer
the new timer
- Timer:set_callback (callback, ...)
-
Set callback function to timer.
Parameters:
- callback function function which is called when timeout occurs
- ... optional additional parameters to callback function
- Timer:set_timeout (timeout)
-
Set timer timeout.
If timeout is nil then timer will stop if it is running
Parameters:
- timeout int timeout in milliseconds
- Timer:set_singleshot (singleshot)
-
Set timer singleshot mode.
Parameters:
- singleshot bool true if timer should only run once
- Timer:start ([timeout])
-
Start timer.
Parameters:
- timeout int in milliseconds, or previously set value if not included (optional)
- Timer:stop ()
- Stop timer.
- Timer.update ()
- Timer update. Class function which calls callback functions for all timers which have timed out and starts timers again for non-singleshot timers