Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

timer doesn't restart after canceling it the first time
Started by nml2727 Nov 06 2019 07:58 AM

- - - - -
6 replies to this topic
timer timer.performwithdelay cancel restart timer

Best Answer nml2727 , 06 November 2019 - 10:46 AM

*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.

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

nml2727

[GLOBAL: userInfoPane.html]
nml2727
  • Enthusiast

  • 96 posts
  • Corona SDK

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?



[TOPIC: post.html]
#2

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,979 posts
  • Corona SDK

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?



[TOPIC: post.html]
#3

nml2727

[GLOBAL: userInfoPane.html]
nml2727
  • Enthusiast

  • 96 posts
  • Corona SDK

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. 



[TOPIC: post.html]
#4

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,979 posts
  • Corona SDK

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

 

 
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()
 


[TOPIC: post.html]
#5

nml2727

[GLOBAL: userInfoPane.html]
nml2727
  • Enthusiast

  • 96 posts
  • Corona SDK

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. 



[TOPIC: post.html]
#6

nml2727

[GLOBAL: userInfoPane.html]
nml2727
  • Enthusiast

  • 96 posts
  • Corona SDK

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?



[TOPIC: post.html]
#7

nml2727

[GLOBAL: userInfoPane.html]
nml2727
  • Enthusiast

  • 96 posts
  • Corona SDK

  Best Answer

*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.




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: timer, timer.performwithdelay, cancel, restart timer