Cancel timer that is in a function in an external module

First the code:

--show ad with counter forward variables M.counterToShowAd = gameSettings.adCounterNumber or 11 M.adBgTmr = nil --show ad with counter function M.showAdWithCounter = function( event ) &nbsp; if M.adBgTmr then &nbsp;&nbsp;&nbsp; timer.cancel( M.adBgTmr ) --Here is my question, Read down &nbsp;&nbsp;&nbsp; print( M.adBgTmr ) &nbsp; end &nbsp; if ( M.counterToShowAd \> 0 ) then &nbsp;&nbsp;&nbsp; M.counterToShowAd = M.counterToShowAd - 1 &nbsp;&nbsp;&nbsp; print("Counting down to show ADS == ", M.counterToShowAd ) &nbsp;&nbsp;&nbsp; loads.adCounterStatus = M.counterToShowAd &nbsp;&nbsp;&nbsp; loads.updateGameProgress() &nbsp; end &nbsp; if ( M.counterToShowAd \<= 0 ) then &nbsp;&nbsp;&nbsp; local cntrX = display.contentCenterX &nbsp;&nbsp;&nbsp; local cntrY = display.contentCenterY &nbsp;&nbsp;&nbsp; ------- &nbsp;&nbsp;&nbsp; local adBg = display.newRect( cntrX, cntrY, 768, 1664 ) &nbsp;&nbsp;&nbsp; adBg:setFillColor( 0, 0, 0 ) &nbsp;&nbsp;&nbsp; M.adBgTmr = timer.performWithDelay(200, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; function() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; display.remove( adBg ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; adBg = nil &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; ) &nbsp;&nbsp;&nbsp; ------- &nbsp;&nbsp;&nbsp; M.showInterstitialAd() &nbsp;&nbsp;&nbsp; loads.adCounterStatus = 10 &nbsp;&nbsp;&nbsp; loads.updateGameProgress() &nbsp; end end

Ok, I’m reading the admob tutorial that has a similar function to show the ad with a number of attempts. Since I’m worried about memoryor errors, I’d like to know what this piece of code is for?

&nbsp; if M.adBgTmr then &nbsp;&nbsp;&nbsp; timer.cancel( M.adBgTmr ) &nbsp;&nbsp;&nbsp; print( M.adBgTmr ) &nbsp; end

Rather I would like to know how is the safest way to run this timer from an external module and be able to cancel it and nil it correctly. I have read but I still can not find the logic of that scoop.

***

What I try to understand is what happens when the module and the function are called. Ex. the module is required, it is read from top to bottom and is ready to be used. In that module the variable M.counterToShowAd takes a value and the variable M.adBgTmr is nil. The function checks if there is a timer assigned to the variable M.adBgTmr, if it is true it is canceled. Then the statements are executed according to the comparison values. Now, if every time I go to a scene I call the module but at the end of the scene I remove everything to start a new scene, will it be necessary to cancel that timer?

I am thinking that this piece of code is unnecessary and could give me errors in some devices in the future.

***

Thanks in advance

DoDi

I think one way to summarize this question is:

How I cancel a timer that is inside a function in an external module?

I think one way to summarize this question is:

How I cancel a timer that is inside a function in an external module?