Skip to content

Commit

Permalink
Implement issue vrld#4: Ability to cancel specific timers
Browse files Browse the repository at this point in the history
  • Loading branch information
vrld committed Mar 31, 2012
1 parent 9d76e4d commit 7c257c8
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions timer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@ local function update(dt)
end

local function add(delay, func)
assert(type(func) == "function", "second argument needs to be a function")
assert(not functions[func], ("Function already scheduled to run in %.2fs"):format(functions[func]))
functions[func] = delay
end

local function cancel(func)
functions[func] = nil
end

local function addPeriodic(delay, func, count)
assert(type(func) == "function", "second argument needs to be a function")
local count = count or math.huge -- exploit below: math.huge - 1 = math.huge

add(delay, function(f)
Expand All @@ -63,7 +66,6 @@ local function clear()
end

local function Interpolator(length, func)
assert(type(func) == "function", "second argument needs to be a function")
local t = 0
return function(dt, ...)
t = t + dt
Expand All @@ -72,7 +74,6 @@ local function Interpolator(length, func)
end

local function Oscillator(length, func)
assert(type(func) == "function", "second argument needs to be a function")
local t = 0
return function(dt, ...)
t = (t + dt) % length
Expand Down

0 comments on commit 7c257c8

Please sign in to comment.