Close a timer opened after function?

Hello,

This is more a variable scope question, but I have a timer being declared/started at the bottom of my app (after all the display and other stuff is declared). And I have a function (near the top) that needs to “cancel” the timer but it cannot cancel nil value error because the variable is not in the same area (?).

How can I solve this without making it a global? Or is making it a global the only way? [import]uid: 46343 topic_id: 18962 reply_id: 318962[/import]

Hey, @dubcanada, forward referencing the variable can save you from tons of headaches.

[lua]local myTimer
local function myFunction()
timer.cancel( myTimer )
end
myTimer = timer.performWithDelay(1000, myFunction)[/lua]

Naomi

Edit: By the way, after I thought about the above, I realize by the time myFunction is called, the timer is already fired. So… I imagine it won’t work the way it is. But still, I suppose the code above can still show you how the forward referencing work. [import]uid: 67217 topic_id: 18962 reply_id: 73041[/import]