Using 2 Timers at once

I am having trouble getting 2 timers to work together. They both seem to execute simultaneously. What am I doing wrong?

See the following code:

[lua]

local function listener2( event )

        gameState.captureVibration = true

end

local function listener( event )

        gameState.captureVibration = false 

end

local function switchIt ()

    timer.performWithDelay( 500, listener )

    timer.performWithDelay( 2000, listener2 )

end

switchIt()
[/lua]

Thanks for the help!!

Hi there,

Your code looks OK to me (aside from the fact that the “event” parameter in listener and listener2 will always be nil, since nothing is getting passed to those functions).

When you say they seem to execute simultaneously, what makes you think so?

Try putting print(“captureVibration off”) in listener and print(“captureVibration on”) in listener2, and then watch the terminal output when you run it.  You should see the print statements appear separately, 1.5 seconds apart.

  • Andrew

Hi there,

Your code looks OK to me (aside from the fact that the “event” parameter in listener and listener2 will always be nil, since nothing is getting passed to those functions).

When you say they seem to execute simultaneously, what makes you think so?

Try putting print(“captureVibration off”) in listener and print(“captureVibration on”) in listener2, and then watch the terminal output when you run it.  You should see the print statements appear separately, 1.5 seconds apart.

  • Andrew