[Resolved] problem with timer:delete

Hi, I’m using the following code to cancel a timer called “Timer4”

stop = function()  
 timer.cancel(Timer4)  
 starTimer1= nil  
end  
  
tmr3 = timer.performWithDelay (30000, stop, 0)  

The problem is that the code works only if I set “tmr3” to a maximum value of 15000 milliseconds. However, if I set it to 30000 (or even 15001) milliseconds it won’t works.

the timer I would like to stop is:

Timer4 = timer.performWithDelay(1700,getRandomPic, 0)  

Any solution?

Thanks!! [import]uid: 159775 topic_id: 28252 reply_id: 328252[/import]

Of course, I would like to mean

Timer4= nil  

instead of

[/code]
starTimer1= nil
[/code] [import]uid: 159775 topic_id: 28252 reply_id: 114150[/import]

Of course, I would like to mean

Timer4= nil  

instead of

starTimer1= nil [import]uid: 159775 topic_id: 28252 reply_id: 114151[/import]

I’m having trouble replicating this. This;

[lua]local count = 0

local function forTimer1()
count = count + 5
print (count… " seconds have passed")
end
timer1 = timer.performWithDelay(5000, forTimer1, 0)

local function killTimer()
timer.cancel(timer1)
timer1 = nil
print “timer1 was killed”
end
timer.performWithDelay(16000, killTimer, 1)[/lua]

Works just fine.

Are you able to provide plug and play demonstrating the issue, please? Will take a look if so.

Peach :slight_smile: [import]uid: 52491 topic_id: 28252 reply_id: 114196[/import]

Sure, here you are:

[code]
local function nuvola()

local starTable = {} – Set up star table

function initStar()
local star1 = {}
star1.imgpath = “nuvola.png”; --Set Image Path for Star
star1.movementSpeed = 10000; --Determines the movement speed of star
table.insert(starTable, star1); --Insert Star into starTable

end --END initStar()

function getRandomStar()
local temp = starTable[math.random(1, #starTable)] – Get a random star from starTable
local randomStar = display.newImage(temp.imgpath) – Set image path for object
randomStar.myName = “star” – Set the name of the object to star
randomStar.movementSpeed = temp.movementSpeed; – Set how fast the object will move
randomStar.x = math.random(0,_W) – Set starting point of star at a random X position
randomStar.y = -50 – Start the star off screen
–randomStar.rotation = math.random(0,360) – Rotate the object
starMove = transition.to(randomStar, {
time=randomStar.movementSpeed,
y=_H,
onComplete = function(self) self.parent:remove(self); self = nil; end
}) – Move the star
end–END getRandomStar()

function startGame()
starTimer4 = timer.performWithDelay(1700,getRandomStar, 0)

end–END startGame()

initStar()
startGame()

end

stop = function()
timer.cancel(starTimer4)
starTimer1= nil
end
tmr2 = timer.performWithDelay (10000, nuvola, -1)
tmr3 = timer.performWithDelay (30000, stop, 0)
[/code] [import]uid: 159775 topic_id: 28252 reply_id: 114215[/import]

Your timer is being cancelled, the issue appears to be that you are calling “nuvola” every 10 seconds and each time you call it you also create another timer. Everything between line 1 and 37 above is being called in that function which you’ve tied to “tmr2” on line 45.

If you were to instead replace line 45 with;
[lua]nuvola()[/lua]

then I believe it would cancel as intended and stars would stop falling. If you wanted to make more stars fall over time you would give the timers unique names so as not to create potentially dozens of timers all named starTimer4, which is what you’re currently doing.

Hope that helps!
Peach :slight_smile: [import]uid: 52491 topic_id: 28252 reply_id: 114275[/import]

Thanks!! You have been very useful! [import]uid: 159775 topic_id: 28252 reply_id: 114285[/import]

Thanks!! You have been very useful! It works fine! [import]uid: 159775 topic_id: 28252 reply_id: 114286[/import]

Not a problem, good luck with your project!

Peach :slight_smile: [import]uid: 52491 topic_id: 28252 reply_id: 114433[/import]