initial commit with all the stuff
This commit is contained in:
50
3B/archive/root_11.bak/scheduler.lua
Normal file
50
3B/archive/root_11.bak/scheduler.lua
Normal file
@@ -0,0 +1,50 @@
|
||||
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
|
||||
Reference in New Issue
Block a user