Difference between revisions of "Timer (event)"

From ComputerCraft Wiki
Jump to: navigation, search
m (Not 'numerical representation', 'unique ID'. os.startTimer() returns an ID.)
(Better explanation and coding practices for first code example. Added second example to deal with multiple timers.)
Line 7: Line 7:
 
}}
 
}}
 
{{Example
 
{{Example
|desc=This code will continuously start an internal timer and fire an event. If the event is not the timer, then it will break the loop
+
|desc=This code will loop and refresh every 1 second. If the event that occurred was a not a timer the loop will complete
|code= while true do<br/>  os.startTimer(1)<br/>  event = os.pullEvent()<br/>  if event == "timer" then <br/>    print("Too slow!")<br/>  else<br/>   print("Darn, you beat me!")<br/>   break<br/> end<br/>end
+
|code=while true do
 +
  os.startTimer(1)
 +
  local event = os.pullEvent()
 +
  if event == "timer" then
 +
    print("Too slow!")
 +
   else
 +
    print("You've beaten me!")
 +
   end
 +
  end
 +
}}
 +
{{Example
 +
|desc=This code will output different text depending on which timer has completed
 +
|code=local timerA = os.startTimer(1)
 +
local timerB = os.startTimer(2)
 +
 +
while true do
 +
  local event, completed = os.pullEvent()
 +
  if event == "timer" then
 +
    if completed == timerA then
 +
      print("TimerA has finished, restarting it now")
 +
      timerA = os.startTimer(1)
 +
    elseif completed == timerB then
 +
      print("TimerB has finished, restarting it now")
 +
      timerB = os.startTimer(2)
 +
    else
 +
      print("A timer finished, but it wasn't one of the ones I care about")
 +
    end
 +
  end
 +
end
 
}}
 
}}

Revision as of 09:38, 28 October 2013

This page needs some serious TLC, stat!
Please help us by cleaning it, fixing it up, or sparing it some love.
(Reason: A demonstration on the use and handling of this event would be beneficial. AfterLifeLochie 16:11, 30 November 2012 (MSK))



Grid Modem.png  Event timer
Fired when a timer completes.
Returned Object 1 The timer's unique ID value (returned when os.startTimer() is called).


Grid paper.png  Example
This code will loop and refresh every 1 second. If the event that occurred was a not a timer 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!")
  end
end



Grid paper.png  Example
This code will output different text depending on which timer has completed
Code
local timerA = os.startTimer(1)
local timerB = os.startTimer(2)

while true do
  local event, completed = os.pullEvent()
  if event == "timer" then
    if completed == timerA then
      print("TimerA has finished, restarting it now")
      timerA = os.startTimer(1)
    elseif completed == timerB then
      print("TimerB has finished, restarting it now")
      timerB = os.startTimer(2)
    else
      print("A timer finished, but it wasn't one of the ones I care about")
    end
  end
end