os.startTimer

From ComputerCraft Wiki
Jump to: navigation, search


Grid Redstone.png  Function os.startTimer
Adds a timer which will fire a single "timer" event after time seconds have passed. It also returns a number representing the unique ID of the timer (which will likewise be included with said event, allowing you to identify it when you pull it).

Fractions of a second are supported, but only down to a game tick, or 1/20th of a second (0.05s) - note that even a timer set for 0 seconds will not fire until at least a tick has passed. Likewise, given that sleep() relies on timer events to function (as do other functions that can wait for a set period of time - eg rednet.receive()), sleep(1.01) and sleep(1.05) would both wait for at least 1.05 seconds.
Syntax os.startTimer(number time)
Returns number timerID
Part of ComputerCraft
API OS

Examples

Grid paper.png  Example
Yields until 3 seconds have passed.
Code
local myTimer = os.startTimer(3)
while true do
  local event, timerID = os.pullEvent("timer")
  if timerID == myTimer then break end
end



Grid paper.png  Example
Waits until 10 seconds have passed, or a key is pressed - whichever happens first.
Code
print("Press any key to continue...")

local myTimer = os.startTimer(10)

while true do
  local event, par1 = os.pullEvent()

  if event == "timer" and par1 == myTimer then
    print("I'm sick of waiting!")
    break
  elseif event == "key" then
    print("You pressed "..keys.getName(par1).."!")
    break
  end
end
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox