Skip to content
Bruno Silvestre edited this page Aug 27, 2014 · 3 revisions

LuaTimer 2.0

Release Notes

  • Move almost all implementation from C to Lua. The C library only retrieves the time in microseconds;
  • Add Windows platform;
  • Use poll of timers.

API

  • luatimer.createpoll()

Create a new poll of timers.

  • poll:create(freq)

Create a new timer with frequence freq (in seconds). Use decimal numbers to precision in milli or microseconds.

  • poll:cancel(t)

Cancel the timer t, removing it from the poll. However, passing the string "all" to this function, all the timers in the poll are cancelled.

  • poll:size()

Return the number of timers in the poll.

  • poll:nextshot()

Return the remain among of time to the next timer expires. If some timer is already expired, the function returns 0 (zero).

  • poll:fired(t)

Return the timers that were expired. If t is the string "all", the function returns a table with all the expired timers. If t is a string "one", the function returns one of the expired timers. In the case that there is no expired times, fired() returns nil.

Exemplo

require("luatimer")

timers = luatimer.createpoll()

t3 = timers:create(3)
t5 = timers:create(5)
t6 = timers:create(6)

print("poll size", timers:size())

print("Next shot: ", timers:nextshot())

-- sleep(4 segundos)

t = timers:fired("one")
print(t)
print(t == t3)

timers:cancel(t3)

print(timers:nextshot())

-- sleep(3 segundos)

tb = timers:fired("all")
for k, t in ipairs(tb) do
   print(t)
end

timers:cancel("all")
Clone this wiki locally