Skip to content

Commit 2549a9a

Browse files
Copilotbennettgoble
andcommitted
Update LLTimers documentation to use :every() instead of :on()
Co-authored-by: bennettgoble <151138+bennettgoble@users.noreply.github.com>
1 parent b2cfa19 commit 2549a9a

1 file changed

Lines changed: 29 additions & 41 deletions

File tree

src/content/docs/script/learn-slua/timers.md

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ SLua provides the `LLTimers` system for managing timers in your scripts. Unlike
99

1010
## Creating Timers
1111

12-
### Recurring Timers with `on()`
12+
### Recurring Timers with `every()`
1313

1414
Create a timer that fires repeatedly at a specified interval:
1515

1616
```lua
17-
local handler = LLTimers:on(0.1, function(scheduled_time, interval)
17+
local handler = LLTimers:every(0.1, function(scheduled_time, interval)
1818
ll.Say(0, "Timer fires every 0.1 seconds")
1919
end)
2020
```
2121

22-
The `on()` method takes two parameters:
22+
The `every()` method takes two parameters:
2323
- **Interval** (number): Time in seconds between timer fires
2424
- **Callback function**: Function called each time the timer fires
2525

@@ -35,24 +35,12 @@ end)
3535

3636
One-time timers automatically remove themselves after firing. Note that the `interval` parameter is not passed to `once()` callbacks since they only fire once.
3737

38-
### Using `every()` as an Alias
39-
40-
The `every()` method is an alias for `on()` and works identically:
41-
42-
```lua
43-
-- These are equivalent
44-
LLTimers:on(0.5, function() end)
45-
LLTimers:every(0.5, function() end)
46-
```
47-
48-
Use whichever reads more naturally in your code.
49-
5038
## Stopping Timers
5139

5240
All timer creation methods return a handler reference that you can use to stop the timer:
5341

5442
```lua
55-
local handler = LLTimers:on(1.0, function()
43+
local handler = LLTimers:every(1.0, function()
5644
ll.Say(0, "Tick")
5745
end)
5846

@@ -69,7 +57,7 @@ The `off()` method returns:
6957
Timer callbacks receive two parameters:
7058

7159
```lua
72-
LLTimers:on(1.0, function(scheduled_time, interval)
60+
LLTimers:every(1.0, function(scheduled_time, interval)
7361
local current_time = os.clock()
7462
local delay = current_time - scheduled_time
7563

@@ -91,17 +79,17 @@ The `scheduled_time` parameter lets you detect if your timer is running late by
9179
Any positive number specifies the interval in seconds:
9280

9381
```lua
94-
LLTimers:on(0.1, callback) -- 100 milliseconds
95-
LLTimers:on(1.0, callback) -- 1 second
96-
LLTimers:on(5.5, callback) -- 5.5 seconds
82+
LLTimers:every(0.1, callback) -- 100 milliseconds
83+
LLTimers:every(1.0, callback) -- 1 second
84+
LLTimers:every(5.5, callback) -- 5.5 seconds
9785
```
9886

9987
### Zero Interval (Immediate Execution)
10088

10189
An interval of `0` creates a timer that fires as soon as possible:
10290

10391
```lua
104-
LLTimers:on(0, function()
92+
LLTimers:every(0, function()
10593
ll.Say(0, "Fires immediately on next tick")
10694
end)
10795
```
@@ -126,7 +114,7 @@ setmetatable(counter, {
126114
end
127115
})
128116

129-
LLTimers:on(1.0, counter)
117+
LLTimers:every(1.0, counter)
130118
```
131119

132120
### Removing Timers from Within Callbacks
@@ -136,7 +124,7 @@ Timers can remove themselves or other timers during execution:
136124
```lua
137125
local handler
138126

139-
handler = LLTimers:on(1.0, function()
127+
handler = LLTimers:every(1.0, function()
140128
ll.Say(0, "Firing once, then stopping")
141129
LLTimers:off(handler)
142130
end)
@@ -147,12 +135,12 @@ You can also modify other timers:
147135
```lua
148136
local timer1, timer2
149137

150-
timer1 = LLTimers:on(1.0, function()
138+
timer1 = LLTimers:every(1.0, function()
151139
ll.Say(0, "Timer 1 stopping timer 2")
152140
LLTimers:off(timer2)
153141
end)
154142

155-
timer2 = LLTimers:on(0.5, function()
143+
timer2 = LLTimers:every(0.5, function()
156144
ll.Say(0, "Timer 2 tick")
157145
end)
158146
```
@@ -162,7 +150,7 @@ end)
162150
Timer callbacks can use `coroutine.yield()` to yield execution back to the simulator:
163151

164152
```lua
165-
LLTimers:on(1.0, function()
153+
LLTimers:every(1.0, function()
166154
ll.Say(0, "Starting")
167155
coroutine.yield() -- Yields to simulator, resumes on next frame
168156
ll.Say(0, "Continued on next frame")
@@ -178,7 +166,7 @@ If a timer is delayed by more than 2 seconds (e.g., due to script lag), it fires
178166
```lua
179167
-- If this timer is delayed by 10 seconds, it won't fire 100 times rapidly
180168
-- Instead, it fires once and resumes normal scheduling
181-
LLTimers:on(0.1, function()
169+
LLTimers:every(0.1, function()
182170
ll.Say(0, "Protected from catch-up spam")
183171
end)
184172
```
@@ -191,17 +179,17 @@ You can create as many timers as you need, each with different intervals:
191179

192180
```lua
193181
-- Fast update for smooth animations
194-
LLTimers:on(0.05, function()
182+
LLTimers:every(0.05, function()
195183
-- Update animation frame
196184
end)
197185

198186
-- Medium update for game logic
199-
LLTimers:on(0.5, function()
187+
LLTimers:every(0.5, function()
200188
-- Check game state
201189
end)
202190

203191
-- Slow update for housekeeping
204-
LLTimers:on(5.0, function()
192+
LLTimers:every(5.0, function()
205193
-- Clean up old data
206194
end)
207195

@@ -220,7 +208,7 @@ All timers run independently and maintain their own schedules.
220208
```lua
221209
local count = 10
222210

223-
local countdown = LLTimers:on(1.0, function()
211+
local countdown = LLTimers:every(1.0, function()
224212
ll.Say(0, tostring(count))
225213
count = count - 1
226214

@@ -249,7 +237,7 @@ delayedSay("Hello in 5 seconds", 5.0)
249237
```lua
250238
local health = 100
251239

252-
LLTimers:on(1.0, function(scheduled_time, interval)
240+
LLTimers:every(1.0, function(scheduled_time, interval)
253241
health = health - 1
254242

255243
if health <= 0 then
@@ -269,7 +257,7 @@ end)
269257
local active = true
270258
local handler
271259

272-
handler = LLTimers:on(1.0, function()
260+
handler = LLTimers:every(1.0, function()
273261
if not active then
274262
return -- Skip execution but keep timer running
275263
end
@@ -296,7 +284,7 @@ function startTimer(name, interval, callback)
296284
end
297285

298286
-- Create and store new timer
299-
timers[name] = LLTimers:on(interval, callback)
287+
timers[name] = LLTimers:every(interval, callback)
300288
end
301289

302290
function stopTimer(name)
@@ -455,12 +443,12 @@ attemptAction(3) -- Try up to 3 times
455443

456444
```lua
457445
-- ✗ Can't stop this timer later
458-
LLTimers:on(1.0, function()
446+
LLTimers:every(1.0, function()
459447
ll.Say(0, "Tick")
460448
end)
461449

462450
-- ✓ Store the handler
463-
local handler = LLTimers:on(1.0, function()
451+
local handler = LLTimers:every(1.0, function()
464452
ll.Say(0, "Tick")
465453
end)
466454
```
@@ -470,7 +458,7 @@ end)
470458
```lua
471459
-- ✗ Creates new timer on each touch, never cleans up old ones
472460
LLEvents:on("touch_start", function(detected)
473-
LLTimers:on(1.0, function()
461+
LLTimers:every(1.0, function()
474462
ll.Say(0, "Timer tick")
475463
end)
476464
end)
@@ -482,7 +470,7 @@ LLEvents:on("touch_start", function(detected)
482470
if handler then
483471
LLTimers:off(handler)
484472
end
485-
handler = LLTimers:on(1.0, function()
473+
handler = LLTimers:every(1.0, function()
486474
ll.Say(0, "Timer tick")
487475
end)
488476
end)
@@ -492,12 +480,12 @@ end)
492480

493481
```lua
494482
-- ✗ Excessive processing for most use cases
495-
LLTimers:on(0.01, function()
483+
LLTimers:every(0.01, function()
496484
-- This fires 100 times per second!
497485
end)
498486

499487
-- ✓ Use reasonable intervals
500-
LLTimers:on(0.1, function()
488+
LLTimers:every(0.1, function()
501489
-- 10 times per second is usually enough
502490
end)
503491
```
@@ -522,7 +510,7 @@ LLTimers:every(5.0, function() ll.Say(0, "Timer 3") end)
522510

523511
The `LLTimers` system provides flexible timer management in SLua:
524512

525-
- Use `LLTimers:on()` or `LLTimers:every()` for recurring timers
513+
- Use `LLTimers:every()` for recurring timers
526514
- Use `LLTimers:once()` for one-time delayed execution
527515
- Use `LLTimers:off()` to stop timers
528516
- Multiple independent timers can run simultaneously

0 commit comments

Comments
 (0)