Some help with understanding timers from type "every x amount of time"

Hey,

What im trying to make is a timer that performs its action every 7 seconds… BUT

Im trying to make it work like this:

I have to variables 

[lua]

local Enable = false

local counter = 0

[/lua]

What im trying to achieve is (in pseudo code) that if any of the condition is failing during the delta time of the 7 seconds, then abort the current timer, and if both condition is met for FULL 7 seconds, then fire

I hope that makes any sense.

[lua]

if (Enable==true and counter==0) then

      Every 7 seconds 

      – make action

end

[/lua]

Ive tried to use the timer.cancel() but still no luck…

Any help will be appreciated 

Roy.

Since you want to keep repeating this every seven seconds, and in some cases perform an action and in other not perform an action, you might want to try using the Runtime enterFrame event. It’s more straightforward / flexible to use for regular events that are dependent on variables. An example of using it to fire an event conditionally every seven seconds is like so

[lua]

local LastUpdate = 0    – static variable
 

local function screenUpdate()   – RunTime enterFrame event handler

    if( this ~= nil ) then    
        local currTime = os.time()    – returns time in seconds
                    –
            – Lets check to see if it’s time to pop out an item
            –
            if( (currTime - LastUpdate) > (60 * 7)  ) then        – Has 7 minutes passed?

                LastUpdate = currTime                                     – Make sure to note the time…

                – Perform an action here… 

                – or not perform, based on some variable settings…
            end           
    end
        
end

– Add the enterFrame listener

Runtime:addEventListener( “enterFrame”, screenUpdate )
 

[/lua]

Sorry, iv’e been away from my mac for the past 3 days, Im gonna try this one out right now, like you said its pretty strait forward with the runtime enterframe event, I was trying to achieve this using the performWithDelay but your way looks a lot better.

Thanks!

Roy. 

Since you want to keep repeating this every seven seconds, and in some cases perform an action and in other not perform an action, you might want to try using the Runtime enterFrame event. It’s more straightforward / flexible to use for regular events that are dependent on variables. An example of using it to fire an event conditionally every seven seconds is like so

[lua]

local LastUpdate = 0    – static variable
 

local function screenUpdate()   – RunTime enterFrame event handler

    if( this ~= nil ) then    
        local currTime = os.time()    – returns time in seconds
                    –
            – Lets check to see if it’s time to pop out an item
            –
            if( (currTime - LastUpdate) > (60 * 7)  ) then        – Has 7 minutes passed?

                LastUpdate = currTime                                     – Make sure to note the time…

                – Perform an action here… 

                – or not perform, based on some variable settings…
            end           
    end
        
end

– Add the enterFrame listener

Runtime:addEventListener( “enterFrame”, screenUpdate )
 

[/lua]

Sorry, iv’e been away from my mac for the past 3 days, Im gonna try this one out right now, like you said its pretty strait forward with the runtime enterframe event, I was trying to achieve this using the performWithDelay but your way looks a lot better.

Thanks!

Roy.