diff --git a/cron.lua b/cron.lua index 0ef5cb9..ff55597 100644 --- a/cron.lua +++ b/cron.lua @@ -36,13 +36,12 @@ end local function updatePeriodicEntry(self, dt) self.running = self.running + dt - if self.running >= self.time then + while self.running >= self.time do self.callback(unpack(self.args)) - self.running=0 + self.running = self.running - self.time end end - local cron = {} function cron.reset() diff --git a/spec/cron_spec.lua b/spec/cron_spec.lua index 6af5081..4598ec9 100644 --- a/spec/cron_spec.lua +++ b/spec/cron_spec.lua @@ -70,16 +70,23 @@ context( 'cron', function() end) test( 'Should execute periodical actions periodically', function() - cron.every(2, count) + cron.every(3, count) cron.update(1) assert_equal(counter, 0) - cron.update(1) + cron.update(2) assert_equal(counter, 1) cron.update(2) - assert_equal(counter, 2) + assert_equal(counter, 1) cron.update(1) assert_equal(counter, 2) end) + + test( 'Should execute the same action multiple times on a single update if appropiate', function() + cron.every(1, count) + cron.update(2) + assert_equal(counter, 2) + end) + end)