mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-10 02:31:48 +00:00
[modified] timer
to support changing the time in reset
This commit is contained in:
parent
89ad4c2dda
commit
41985928d2
10
timer.lua
10
timer.lua
@ -17,11 +17,11 @@ local timer = class()
|
|||||||
-- the timer object, so can be reset if needed
|
-- the timer object, so can be reset if needed
|
||||||
function timer:new(time, on_progress, on_finish)
|
function timer:new(time, on_progress, on_finish)
|
||||||
return self:init({
|
return self:init({
|
||||||
time = math.max(time, 1e-6), --negative time not allowed
|
time = 0, --set in the reset below
|
||||||
timer = 0,
|
timer = 0,
|
||||||
on_progress = on_progress,
|
on_progress = on_progress,
|
||||||
on_finish = on_finish,
|
on_finish = on_finish,
|
||||||
})
|
}):reset(time)
|
||||||
end
|
end
|
||||||
|
|
||||||
--update this timer, calling the relevant callback if it exists
|
--update this timer, calling the relevant callback if it exists
|
||||||
@ -50,10 +50,12 @@ function timer:progress()
|
|||||||
return math.min(self.timer / self.time, 1)
|
return math.min(self.timer / self.time, 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
--reset the timer
|
--reset the timer; optionally change the time
|
||||||
--will resume calling the same callbacks, so can be used for intervals
|
--will resume calling the same callbacks, so can be used for intervals
|
||||||
function timer:reset()
|
function timer:reset(time)
|
||||||
self.timer = 0
|
self.timer = 0
|
||||||
|
self.time = math.max(time or self.time, 1e-6) --negative time not allowed
|
||||||
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
return timer
|
return timer
|
||||||
|
Loading…
Reference in New Issue
Block a user