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 ) if M.adBgTmr then timer.cancel( M.adBgTmr ) --Here is my question, Read down print( M.adBgTmr ) end if ( M.counterToShowAd \> 0 ) then M.counterToShowAd = M.counterToShowAd - 1 print("Counting down to show ADS == ", M.counterToShowAd ) loads.adCounterStatus = M.counterToShowAd loads.updateGameProgress() end if ( M.counterToShowAd \<= 0 ) then local cntrX = display.contentCenterX local cntrY = display.contentCenterY ------- local adBg = display.newRect( cntrX, cntrY, 768, 1664 ) adBg:setFillColor( 0, 0, 0 ) M.adBgTmr = timer.performWithDelay(200, function() display.remove( adBg ) adBg = nil end ) ------- M.showInterstitialAd() loads.adCounterStatus = 10 loads.updateGameProgress() 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?
if M.adBgTmr then timer.cancel( M.adBgTmr ) print( M.adBgTmr ) 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