Skip to content

Commit 2123a41

Browse files
authored
Merge pull request #178 from Demigod916/timer
lib.timer Docs
2 parents 336b4a7 + 037f03d commit 2123a41

File tree

1 file changed

+160
-0
lines changed

1 file changed

+160
-0
lines changed

pages/ox_lib/Modules/Timer/Shared.mdx

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# Shared
2+
3+
Provides a versatile timer system with options for asynchronous operation, pause and resume functionality, and callbacks on timer completion.
4+
5+
## Timer
6+
7+
### lib.timer
8+
9+
```lua
10+
lib.timer(time, onEnd, async)
11+
```
12+
13+
- time: `number`
14+
- onEnd: `function`
15+
- async?: `boolean`
16+
- If `true` then the timer does not block script execution on the calling thread.
17+
18+
Returns:
19+
20+
- timer: `OxTimer`
21+
22+
#### Example
23+
```lua
24+
25+
local timer = lib.timer(5000, function()
26+
print("timer ended")
27+
end)
28+
```
29+
30+
## Methods
31+
32+
### pause
33+
34+
Pauses an active timer until `timer:play()` or `timer:forceEnd()` is called.
35+
36+
```lua
37+
timer:pause()
38+
```
39+
40+
#### Example
41+
```lua
42+
local timer = lib.timer(5000, function()
43+
print("timer ended")
44+
end, true)
45+
46+
timer:pause()
47+
```
48+
49+
### play
50+
Resume a timer if it is paused with `timer:pause()`.
51+
52+
```lua
53+
timer:play()
54+
```
55+
56+
#### Example
57+
```lua
58+
local timer = lib.timer(5000, function()
59+
print("timer ended")
60+
end, true)
61+
62+
timer:pause()
63+
64+
Wait(1000)
65+
66+
timer:play()
67+
--timer finishes in 6 seconds rather than 5 because of the pause
68+
```
69+
70+
### forceEnd
71+
72+
Immediately ends the timer and optionally triggers the onEnd callback.
73+
74+
```lua
75+
timer:forceEnd(triggerOnEnd)
76+
```
77+
78+
- triggerOnEnd: `boolean`
79+
80+
#### Example
81+
```lua
82+
local timer = lib.timer(5000, function()
83+
print("timer ended")
84+
end, true)
85+
86+
timer:pause()
87+
88+
Wait(1000)
89+
90+
timer:forceEnd(false)
91+
--timer finishes in 1 second rather than 5 because of the forceEnd and the call back never runs
92+
```
93+
94+
### isPaused
95+
Checks if the timer is paused from calling `timer:pause()` previously.
96+
97+
```lua
98+
timer:isPaused()
99+
```
100+
101+
Returns:
102+
- isPaused: `boolean`
103+
104+
#### Example
105+
```lua
106+
local timer = lib.timer(5000, function()
107+
print("timer ended")
108+
end, true)
109+
110+
print(timer:isPaused()) -- false
111+
112+
timer:pause()
113+
114+
print(timer:isPaused()) -- true
115+
```
116+
117+
### getTimeLeft
118+
Returns the remaining time on the timer in the given format rounded to 2 decimal places
119+
120+
```lua
121+
timer:getTimeLeft(format)
122+
-- format: 'ms' = miliseconds, 's' = seconds, 'm' = minutes, 'h' = hours, nil = all returned in a table
123+
```
124+
- format?: `'ms'` or `'s'` or `'m'` or `'h'`
125+
126+
Returns:
127+
- time: `number` | `{ms: number, s: number, m: number, h: number}`
128+
129+
#### Example
130+
```lua
131+
local timer = lib.timer(5000, function()
132+
print("timer ended")
133+
end, true)
134+
135+
print(timer:getTimeLeft('ms')) -- 5000 miliseconds
136+
print(timer:getTimeLeft('s')) -- 5.00 seconds
137+
print(timer:getTimeLeft('m')) -- 0.08 minutes
138+
print(timer:getTimeLeft('h')) -- 0.00 hours
139+
print(timer:getTimeLeft()) -- {ms = 5000, s = 5.00, m = 0.08, h = 0.00 }
140+
```
141+
142+
### restart
143+
Resets and starts the timer.
144+
145+
```lua
146+
timer:restart()
147+
```
148+
149+
#### Example
150+
151+
```lua
152+
-- this will create a timer that just keeps restarting itself
153+
local timer
154+
155+
timer = lib.timer(5000, function()
156+
print("timer ended")
157+
timer:restart()
158+
end, true)
159+
160+
```

0 commit comments

Comments
 (0)