Timers & Intervals
Timers work in persistent scripts (started with lua:start). They fire on the main thread during the render frame.
Recurring Timer
Section titled “Recurring Timer”local id = pl.set_interval(1000, function() pl.log("Every second")end)Returns a timer ID. The callback fires every ms milliseconds.
One-Shot Timer
Section titled “One-Shot Timer”local id = pl.set_timeout(5000, function() pl.log("5 seconds later")end)Fires once after ms milliseconds, then auto-removes.
Cancel a Timer
Section titled “Cancel a Timer”pl.clear_interval(id)pl.clear_timeout(id) -- same function, works for bothExample: Auto-Save Reminder
Section titled “Example: Auto-Save Reminder”pl.set_interval(300000, function() -- 5 minutes pl.log("Reminder: save your project!")end)Example: Delayed Action
Section titled “Example: Delayed Action”-- Start recording, then stop after 8 barspl.cmd("transport:toggle-record")
local transport = pl.get_transport()local bar_duration_ms = (60000 / transport.tempo) * transport.timeSigNum * 8
pl.set_timeout(bar_duration_ms, function() pl.cmd("transport:stop") pl.log("Recording stopped after 8 bars")end)Performance
Section titled “Performance”- Timers are checked each render frame (~16ms at 60fps)
- Timer resolution is frame-rate dependent, not sample-accurate
- For sample-accurate timing, use
pl.on_beatorpl.on_barinstead - One-shot timers are automatically cleaned up after firing