Jump to content




Os.starttimer(x) Return Time Left


  • You cannot reply to this topic
1 reply to this topic

#1 CCJJSax

  • Members
  • 262 posts

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.

#2 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

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.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users