Creating timed loop?

There was the example in java that showed how exactly script work.

Input:
Current time
Output:
One-minute delay “Hello World”

I know that I will need these functions in order to make this work…

Os.time
If/while/loop
Varible that hold exactly time script runs

!= doesn’t work for codea.

What could?

It is ~= in Lua instead of !=

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

i’ll add it to my repo: http://jmv38.comze.com/CODEAbis/server.php#forumTopics

That’s a neat example, thanks for sharing.