timer doesn't restart after canceling it the first time

I create a timer which calls a function over and over until I cancel the timer.  

It works perfectly the first time. 

But when I try to call the function again it doesn’t work…  Only works the first time.

Here’s the timer code.

local flashing\_timer local cancel\_timer local function stopFlashing() if(flashing\_timer ~= nil) then timer.cancel(flashing\_timer) end end local function flashing() -- THIS CODE ONLY EXECUTES THE FIRST TIME TIMER IS STARTED end local function startFlashing() flashing\_timer = timer.performWithDelay(200, flashing, -1) cancel\_timer = timer.performWithDelay(3000, stopFlashing) end

Basically I am calling the startFlashing() function from another module occasionally.  The very first time I call it, the code in the flashing() function executes just fine, until it is cancelled by the stopFlashing() function.  However, once I try to call startFlashing() again, it never runs the code in flashing()… 

Anyone see anything wrong with the code?

Works fine for me in a blank project. Are you sure the other module actually has access to startFlashing? If you put a print statement in there does it fire?

Yeah, the print statement fires the first time but then never again unless I relaunch the project. 

Actually to clarify, the print statement inside of startFlashing runs every time I call that method.  But the statement inside of flashing only runs the first time before the timer is cancelled. 

Strange one - I modified I slightly to keep restarting itself, seems to work fine…

[lua]

local flashing_timer

local cancel_timer

local startFlashing

local function stopFlashing()

   if(flashing_timer ~= nil) then

      timer.cancel(flashing_timer)

       print (“cancel”)

   end

   

   startFlashing()

end

local function flashing()

   – THIS CODE ONLY EXECUTES THE FIRST TIME TIMER IS STARTED

   print (“flash”)

end

startFlashing = function()

   flashing_timer = timer.performWithDelay(200, flashing, -1)

   cancel_timer = timer.performWithDelay(3000, stopFlashing)

   print (“start”)

end

startFlashing()

[/lua]

So it works in blank project, but not in my project.  

I have this timer code in part of my player_maker.lua which creates the player at the start of the game.  It also destroys the player and recreates the player when I restart the level.  Could something be happening when I destroy and recreate the player that is affecting the flashing function?  Like somehow it’s not pointing to the same function that it was when the game first started?  

My code is too long to post it all but that’s the only thing that I can think is different from running it in a blank project. 

I’m not even sure where else I can add a print statement to help clarify what is happening. 

All the print statements work just as expected, “start”, “cancel”, “flash” the fist time.  And then when I try to restart the level then I only see the “start” one.  So “flash” and “cancel” don’t get called after a restart.  So it means the timer somehow becomes nil before stopFlashing gets called?

*facepalm*     Just found the problem…    In another part of my code that was supposed to be flashing_flag = false, I had put flashing = false…  

Sorry for being an idiot.   :blink:  Thanks for the help regardless.  Corona folks have been the most helpful of any community I’ve come across.