app.timer - Timer API
Provides delayed execution and repeated execution functionality.
Methods
app.timer.after(seconds, callback)
Creates a one-shot delayed timer.
Parameters:
seconds(number) - Delay time in seconds, minimum 0.01callback(function) - Callback function to execute after the delay
Returns: string, error - Timer ID
-- Execute after 3 seconds
local id, err = app.timer.after(3, function()
app.notification.show("Reminder", "3 seconds are up!")
end)
-- Execute after 0.5 seconds
app.timer.after(0.5, function()
app.log.info("Executed after a short delay")
end)
app.timer.start(seconds, callback)
Creates a repeating timer.
Parameters:
seconds(number) - Execution interval in seconds, minimum 0.1callback(function) - Callback function to execute on each trigger
Returns: string, error - Timer ID
-- Execute every 5 seconds
local id = app.timer.start(5, function()
app.log.info("Heartbeat: " .. os.date())
end)
-- Stop when needed
app.timer.stop(id)
app.timer.stop(id)
Cancels the specified timer.
Parameters:
id(string) - Timer ID
Returns: boolean, error
local ok, err = app.timer.stop(id)
app.timer.stopAll()
Cancels all timers for the current plugin.
Returns: boolean
app.timer.stopAll()
app.timer.list()
Lists all active timers for the current plugin.
Returns: array<table> - Each item contains:
id(string) - Timer IDtype(string) - Type:"once"or"repeating"interval(number) - Interval/delay in secondsrepeats(boolean) - Whether it repeatsremaining(number, one-shot only) - Remaining time in seconds
local timers = app.timer.list()
for _, tmr in ipairs(timers) do
app.log.info(tmr.id .. ": " .. tmr.type .. " interval=" .. tmr.interval)
end
Description
- Each plugin can have up to 100 timers
- One-shot timers are automatically removed after execution
- Timers are automatically cancelled when the plugin is unloaded
- Callbacks are invoked on the plugin execution queue (thread-safe)
after()minimum delay is 0.01 seconds,every()minimum interval is 0.1 seconds
Examples
Periodically check for file changes
local checkTimerId = nil
function MyPlugin:handleStartMonitor(context)
local dir = context.currentDirectory
local lastCount = 0
checkTimerId = app.timer.start(10, function()
local files = app.file.list(dir)
if files and #files ~= lastCount then
local diff = #files - lastCount
if diff > 0 then
app.notification.show("New Files", "Found " .. diff .. " new file(s)")
end
lastCount = #files
end
end)
app.notification.show("Monitor", "Started monitoring: " .. dir)
end
function MyPlugin:handleStopMonitor(context)
if checkTimerId then
app.timer.stop(checkTimerId)
checkTimerId = nil
app.notification.show("Monitor", "Monitoring stopped")
end
end
Auto-close progress bar after delay
function MyPlugin:handleProcess(context)
app.progress.show("Processing", {message = "Processing..."})
-- Simulate processing
for i = 1, 100 do
app.progress.update(i, "Step " .. i .. "/100")
app.thread.sleep(0.05)
end
app.progress.update(100, "Done!")
-- Auto-close progress bar after 1 second
app.timer.after(1, function()
app.progress.hide()
end)
end