Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

"timer.cancel()" doesn't work when trying to stop a function called with "timer.performWithDelay()".
Started by sheikhameen123 Mar 15 2019 04:28 PM

3 replies to this topic

Best Answer schizoid2k , 15 March 2019 - 04:37 PM

Scope.

 

spawnTimer is local to the funciton and is created/init'd every time you enter that function.

If you declare spawnTimer outside of the function, it will work.

 

--john

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

sheikhameen123

[GLOBAL: userInfoPane.html]
sheikhameen123
  • Observer

  • 6 posts
  • Corona SDK

It says invalid timer (expected a table but got a nil)

local autoToggle = false

local autoButton = display.newCircle( ccx, ccy, 10)
autoButton:setFillColor(0.07)
autoButton.x = cw - 60
autoButton.y = -10


local function auto()
                local shape = display.newCircle( ccx, 30, 5 )
                shape:setFillColor(1,1,1)
                physics.addBody( shape, "dynamic", {density=1, radius=6})
                
                local function removeCircle()
                        display.remove(shape)
                end
                timer.performWithDelay( 10000, removeCircle)
end

local function autoToggleFunction()
        if ( autoToggle == false ) then
                local spawnTimer = timer.performWithDelay(10, auto, -1)
                autoToggle = true
        elseif ( autoToggle == true ) then
                timer.cancel( spawnTimer )
                autoToggle = false
        end
end

autoButton:addEventListener("tap", autoToggleFunction)

Trying to execute function autoToggleFunction() with delays when the shape(autoButton) is "tapped".

And stop the timer when it is clicked again.

It says invalid timer. How do I fix this?

Also, is there any ways to simplify this code?



[TOPIC: post.html]
#2

schizoid2k

[GLOBAL: userInfoPane.html]
schizoid2k
  • Contributor

  • 555 posts
  • Corona SDK

  Best Answer

Scope.

 

spawnTimer is local to the funciton and is created/init'd every time you enter that function.

If you declare spawnTimer outside of the function, it will work.

 

--john


  • sheikhameen123 likes this

[TOPIC: post.html]
#3

sheikhameen123

[GLOBAL: userInfoPane.html]
sheikhameen123
  • Observer

  • 6 posts
  • Corona SDK

Scope.

 

spawnTimer is local to the funciton and is created/init'd every time you enter that function.

If you declare spawnTimer outside of the function, it will work.

 

--john

OMG! THANK YOU SO MUCH JOHN!!!!!


  • schizoid2k likes this

[TOPIC: post.html]
#4

schizoid2k

[GLOBAL: userInfoPane.html]
schizoid2k
  • Contributor

  • 555 posts
  • Corona SDK

Glad I could help!  :)




[topic_controls]
[/topic_controls]