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

[1.4] os.startTimer(time[, name]), os.stopTimer(name)

Started by ardera, 27 June 2012 - 02:02 PM
ardera #1
Posted 27 June 2012 - 04:02 PM
In my program, there is a timer in a function, and if I restart the function, sometimes comes the Error that the timer event was fired. But: The timer started in the call of the function before, and there is no option to stop this timer and to prevent that the timer event is fired anyway. And I cant wait 10 secs before I start the function again. Then I had an idea to prevent all that: os.startTimer could have a second Variable to the Name (String) Variable. And with os.stopTimer(name) you can delete this Timer. Its would be very useful for me and probably some other coders.
MysticT #2
Posted 27 June 2012 - 04:36 PM
I don't think it's needed. Tell us what you'r trying to do, cause there must be a way to do it.
Xfel #3
Posted 27 June 2012 - 06:59 PM
I think his problem is that you can't remove a timer; it will continue posting events forever.
MysticT #4
Posted 27 June 2012 - 07:34 PM
But you can just ignore the event, what's the problem with that?
ardera #5
Posted 27 June 2012 - 08:00 PM
But you can just ignore the event, what's the problem with that?
Because I am waiting for the timer event that I started in the actual function call, and not in the function call before. I can't ignore the event because its needed.
MysticT #6
Posted 27 June 2012 - 08:37 PM
But you have to check if the timer you get is the correct one. So, store the timer in a variable, and use it to compare:

local timer

local function startTimer(n)
  timer = os.startTimer(n)
end

startTimer(1)
while true do
  local evt, arg = os.pullEvent("timer")
  if arg == timer then
    print("Time's out")
    break
  end
end
Cloudy #7
Posted 28 June 2012 - 11:20 AM
I think his problem is that you can't remove a timer; it will continue posting events forever.

Except it only posts an event once.
ardera #8
Posted 28 June 2012 - 02:27 PM
@MysticT: do you really think it works? os.startTimer() returns an empty table and the arg of the event is a number (the time)

Thx works :P/>/>
Edited on 28 June 2012 - 12:51 PM
MysticT #9
Posted 28 June 2012 - 06:04 PM
@MysticT: do you really think it works? os.startTimer() returns an empty table and the arg of the event is a number (the time)

Thx works :P/>/>
Yes, the empty table is just a token to idetify the timer, since tables are guaranteed to be unique.