Stopwatch
一个时间计算lua
new
local stopwatch = Stopwatch:new()
hasTimeElapsed
stopwatch:hasTimeElapsed(long ms) : boolean
hasTimeElapsedRest
stopwatch:hasTimeElapsedRest(long ms, boolean reset) : boolean
getElapsedTime
stopwatch:getElapsedTime() : long
reset
stopwatch:reset()
Stopwatch = {}
Stopwatch.__index = Stopwatch
function Stopwatch:new()
local self = setmetatable({}, Stopwatch)
self.lastMS = os.time() * 1000
return self
end
function Stopwatch:hasTimeElapsed(time)
return os.time() * 1000 - self.lastMS > time
end
function Stopwatch:hasTimeElapsedRest(time, reset)
if self:hasTimeElapsed(time) then
if reset then
self:reset()
end
return true
end
return false
end
function Stopwatch:getElapsedTime()
return os.time() * 1000 - self.lastMS
end
function Stopwatch:reset()
self.lastMS = os.time() * 1000
end
function Stopwatch:reset(time)
self.lastMS = time
end