How to cancel a timer but not in the listener function?

I have some stuff in the screen and a button at topleft corner. I have a timer (timer1) to do animations and when I press the button I want to stop that timer. I made that:


local button=newImage(“button.png”, 0, 0)

function animations()
    – do animations
end

local timer1=timer.performWithDelay( 100, animations, 0)

local function buttonPressed()

   timer.cancel(timer1)

   return true

end

button:addEventListener( “tap”, buttonPressed )


When I press the button in the simulator, it says “Corona Runtime Error” - attempt to index a nil value. It points to the line:     timer.cancel(timer1)

What is the problem?

apparently this isnt code from ur project or we will need to see more code cause this code works when put in its own project file

I fixed it!

The problem was declaring timer1 as local instead of global

Now it works.

It cannot be error and you just ‘polluted’ your global space with timer1 - just it

apparently this isnt code from ur project or we will need to see more code cause this code works when put in its own project file

I fixed it!

The problem was declaring timer1 as local instead of global

Now it works.

It cannot be error and you just ‘polluted’ your global space with timer1 - just it