Difference between revisions of "Os.startTimer"

From ComputerCraft Wiki
Jump to: navigation, search
(Added NeedsWork tag)
m
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{NeedsWork|This page needs an example}}
 
 
{{lowercase}}
 
{{lowercase}}
 
{{Function
 
{{Function
|name=os.startTimer
+
|name=os.startTimer
|args={{type|number}} time
+
|args={{type|number}} time
|api=OS
+
|api=OS
|returns={{type|number}} timerID
+
|returns={{type|number}} timerID
|addon=ComputerCraft
+
|addon=ComputerCraft
|desc=Adds a timer which will fire a "timer" event once after <var>time</var> seconds have passed. It returns an int which acts as a unique ID for the timer.
+
|desc=Adds a timer which will fire a single [[Timer_(event)|"timer" event]] after <var>time</var> 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 [[os.pullEvent|pull it]]).<br><br>
|examples=
+
 
 +
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 [[os.sleep|sleep()]] relies on timer events to function (as do other functions that can wait for a set period of time - eg [[rednet.receive|rednet.receive()]]), sleep(1.01) and sleep(1.05) would both wait for at least 1.05 seconds.
 +
|examples=
 +
{{Example
 +
|desc=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
 +
}}
 +
{{Example
 +
|desc=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
 +
}}
 
}}
 
}}
  
 
[[Category:Lua_Core_Functions]]
 
[[Category:Lua_Core_Functions]]

Latest revision as of 13:24, 17 December 2014


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