From 0c1f7a5e88bca05bd868eaf3da28d922d455637e Mon Sep 17 00:00:00 2001 From: Tanner Rogalsky Date: Tue, 11 Jun 2013 12:41:46 -0400 Subject: [PATCH] Add doFor function that runs the callback on every update until the timer expires. --- cron.lua | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/cron.lua b/cron.lua index 25b8013..3cbac8a 100644 --- a/cron.lua +++ b/cron.lua @@ -60,6 +60,15 @@ local function updatePeriodicEntry(self, dt) 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 tags = {...} local len = #tags @@ -151,6 +160,11 @@ function cron.every(time, callback, ...) return newEntry(time, callback, updatePeriodicEntry, ...) end +function cron.doFor(time, callback, ...) + checkTimeAndCallback(time, callback) + return newEntry(time, callback, updateLimitedEntry, ...) +end + function cron.update(dt) assert(type(dt) == "number" and dt >= 0, "dt must be a non-negative number") @@ -174,4 +188,3 @@ end cron.reset() return cron -