Difference between revisions of "Timer (event)"
From ComputerCraft Wiki
(Better explanation and coding practices for first code example. Added second example to deal with multiple timers.) |
Bomb Bloke (Talk | contribs) m (Page has an example, no longer needs a request for one.) |
||
| Line 1: | Line 1: | ||
| − | |||
| − | |||
{{Event | {{Event | ||
|name=timer | |name=timer | ||
|desc=Fired when a timer completes. | |desc=Fired when a timer completes. | ||
| − | |return1=The timer's unique ID value (returned when [[os.startTimer|os.startTimer()]] is called). | + | |return1=The timer's unique ID value (returned when [[os.startTimer|os.startTimer()]], which triggers this event, is called). |
}} | }} | ||
{{Example | {{Example | ||
| − | |desc=This code will loop and refresh every 1 second. If the event that occurred was a not a timer the loop will complete | + | |desc=This code will loop and refresh every 1 second. If the event that occurred was a not a timer (eg a [[Key_%28event%29|key event]] from a keypress), the loop will complete. |
|code=while true do | |code=while true do | ||
os.startTimer(1) | os.startTimer(1) | ||
| Line 15: | Line 13: | ||
else | else | ||
print("You've beaten me!") | print("You've beaten me!") | ||
| + | break | ||
end | end | ||
end | end | ||
}} | }} | ||
{{Example | {{Example | ||
| − | |desc=This code will output different text depending on which timer has completed | + | |desc=This code will output different text depending on which timer has completed. |
|code=local timerA = os.startTimer(1) | |code=local timerA = os.startTimer(1) | ||
local timerB = os.startTimer(2) | local timerB = os.startTimer(2) | ||
Latest revision as of 19:52, 19 December 2013
| Fired when a timer completes. | |
| Returned Object 1 | The timer's unique ID value (returned when os.startTimer(), which triggers this event, is called). |
| This code will loop and refresh every 1 second. If the event that occurred was a not a timer (eg a key event from a keypress), the loop will complete. | |
| Code |
while true do
os.startTimer(1)
local event = os.pullEvent()
if event == "timer" then
print("Too slow!")
else
print("You've beaten me!")
break
end
end
|