51 lines
957 B
Lua
51 lines
957 B
Lua
local scheduler = {}
|
|
|
|
socket = require('socket')
|
|
|
|
function scheduler.Timer(init)
|
|
-- the new instance
|
|
|
|
local self = {
|
|
-- public fields go in the instance table
|
|
interval = init
|
|
}
|
|
|
|
local function millis()
|
|
return socket.gettime()*1000
|
|
end
|
|
|
|
-- private fields are implemented using locals
|
|
-- they are faster than table access, and are truly private,
|
|
-- so the code that uses your class can't get them
|
|
|
|
local _timestamp = millis()
|
|
local _running = true
|
|
|
|
function self.elapsed()
|
|
if _running and (millis() - _timestamp ) >= self.interval then
|
|
_running = false
|
|
return true
|
|
else
|
|
return false
|
|
end
|
|
end
|
|
|
|
function self.running()
|
|
return (millis() - _timestamp ) < self.interval
|
|
end
|
|
|
|
function self.restart()
|
|
_timestamp = millis()
|
|
_running = true
|
|
end
|
|
|
|
function self.count()
|
|
return millis() - timestamp
|
|
end
|
|
|
|
-- return the instance
|
|
return self
|
|
end
|
|
|
|
return scheduler
|