This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
CCJJSax's profile picture

Os.starttimer(x) Return Time Left

Started by CCJJSax, 24 November 2013 - 02:01 PM
CCJJSax #1
Posted 24 November 2013 - 03:01 PM
Is there a way directly using os.startTimer() to return how much time remains until it's done?

If not, what do you suggest? I have some ideas of how to do it that would work, but I want an efficient way.
Kingdaro #2
Posted 24 November 2013 - 03:12 PM
You could use os.clock(), then add the length of the timer to get when the timer will end. Then when you need to check how much time you have left, subtract the end time and the current time (os.clock()). Here's an example:


local period = 10
local timer = os.startTimer(period)
local endTime = os.clock() + period

while true do
   local ev, p1 = os.pullEvent()
   if ev == "key" then
      print("Time left: ", endTime - os.clock())
   elseif ev == "timer" and p1 == timer then
      break
   end
end

You can check the time left by pressing any key, and the loop breaks once the time is up.