Add doFor function that runs the callback on every update until the timer expires.

This commit is contained in:
Tanner Rogalsky 2013-06-11 12:41:46 -04:00
parent b0127a3f14
commit 0c1f7a5e88

View File

@ -60,6 +60,15 @@ local function updatePeriodicEntry(self, dt)
end end
end end
local function updateLimitedEntry(self, dt)
self.running = self.running + dt
self.callback(unpack(self.args))
if self.running >= self.time then
cron.cancel(self)
end
end
local function addTags(...) local function addTags(...)
local tags = {...} local tags = {...}
local len = #tags local len = #tags
@ -151,6 +160,11 @@ function cron.every(time, callback, ...)
return newEntry(time, callback, updatePeriodicEntry, ...) return newEntry(time, callback, updatePeriodicEntry, ...)
end end
function cron.doFor(time, callback, ...)
checkTimeAndCallback(time, callback)
return newEntry(time, callback, updateLimitedEntry, ...)
end
function cron.update(dt) function cron.update(dt)
assert(type(dt) == "number" and dt >= 0, "dt must be a non-negative number") assert(type(dt) == "number" and dt >= 0, "dt must be a non-negative number")
@ -174,4 +188,3 @@ end
cron.reset() cron.reset()
return cron return cron