This ‘timer and loop’ question comes quite ofter to newbes. Here is a a short example:
--# Main
-- Timer function
function setup()
myTimer1 = Timer("one shot timer", helloFunction )
myTimer2 = Timer("loop", helloFunction ,"infiniteLoop")
myTimer1:start(3)
myTimer2:start(2)
end
function draw()
myTimer1:update()
myTimer2:update()
end
function helloFunction(id)
print("Hello World! from "..id.." @ "..tostring(ElapsedTime).." s")
end
Timer = class()
function Timer:init(id,callBackFun,mode)
self.id = id
self.action = callBackFun
self.stopTime = 0
self.enable = false
self.oneShot = (mode~="infiniteLoop")
end
function Timer:start(delay)
self.stopTime = ElapsedTime + delay
self.delay = delay
self.enable = true
end
function Timer:update()
if (self.enable == true) then
if ElapsedTime >= self.stopTime then
if self.oneShot then self.enable = false
else self:start(self.delay)
end
if type(self.action) == "function" then
self.action(self.id)
end
end
end
end