cron.lua are a set of functions for executing actions at a certain time interval.
local clock = cron.after(time, callback, ...).
Creates a clock that will execute callback after time passes. If additional params were provided, they are passed to callback.
local clock = cron.every(time, callback, ...).
Creates a clock that will execute callback every time, periodically. Additional parameters are passed to the callback too.
Clock methods:
local expired = clock:update(dt).
Increases the internal timer in the clock by dt.
- On one-time clocks, if the internal timer surpasses the clock's
time, then the clock'scallbackis invoked. - On periodic clocks, the
callbackis executed 0 or more times, depending on how bigdtis and the clock's internal timer. expiredwill be true for one-time clocks whose time has passed, so their function has been invoked.
clock:reset([running])
Changes the internal timer manually to running, or to 0 if nothing is specified. It never invokes callback.
localcron=require'cron'localfunctionprintMessage()
print('Hello')
end-- the following calls are equivalent:localc1=cron.after(5, printMessage)
localc2=cron.after(5, print, 'Hello')
c1:update(2) -- will print nothing, the action is not done yetc1:update(5) -- will print 'Hello' oncec1:reset() -- reset the counter to 0-- prints 'hey' 5 times and then prints 'hello'whilenotc1:update(1) doprint('hey')
end-- Create a periodical clock:localc3=cron.every(10, printMessage)
c3:update(5) -- nothing (total time: 5)c3:update(4) -- nothing (total time: 9)c3:update(12) -- prints 'Hello' twice (total time is now 21)cron.luadoes not implement any hardware or software clock; you will have to provide it with the access to the hardware timers, in the form of periodic calls tocron.updatecrondoes not have any defined time units (seconds, milliseconds, etc). You define the units it uses by passing it adtoncron.update. Ifdtis in seconds, thencronwill work in seconds. Ifdtis in milliseconds, thencronwill work in milliseconds.
Just copy the cron.lua file somewhere in your projects (maybe inside a /lib/ folder) and require it accordingly.
Remember to store the value returned by require somewhere! (I suggest a local variable named cron)
localcron=require'cron'Also, make sure to read the license file; the text of that license file must appear somewhere in your projects' files.
This project uses busted for its specs. If you want to run the specs, you will have to install it first. Then run:
cd path/where/the/spec/folder/is
busted