[Resolved]can't cancel timer

Hi, I’ve tried to follow this guide (http://developer.anscamobile.com/reference/index/timercancel)

to restart a timer, but it doesn’t word.

My timer name is “tmr”. How can I cancel it?

Thanks [import]uid: 129238 topic_id: 26077 reply_id: 326077[/import]

Try running this;
[lua]local timerCount = 0

local function timerFunc()
print “Timer fired!”
timerCount = timerCount + 1
if timerCount == 10 then
timer.cancel(tmr)
print “Timer cancelled!”
end
end

tmr = timer.performWithDelay(1000, timerFunc, -1)[/lua]

It should fire 10 times then cancel itself.

Peach :slight_smile: [import]uid: 52491 topic_id: 26077 reply_id: 105608[/import]

thanks, I’ve tried several ways but my code still doesn’t work :frowning:

Here it is:

local function star()  
  
local starTable = {} -- Set up star table  
   
function initStar()  
 local star1 = {}  
 star1.imgpath = "star1.png"; --Set Image Path for Star  
 star1.movementSpeed = 10000; --Determines the movement speed of star  
 table.insert(starTable, star1); --Insert Star into starTable  
  
 local star2 = {}  
 star2.imgpath = "star2.png";  
 star2.movementSpeed = 12000;  
 table.insert(starTable, star2);   
  
  
 local star3 = {}  
 star3.imgpath = "star3.png";  
 star3.movementSpeed = 14000;  
 table.insert(starTable, star3);  
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()  
 starTimer1 = timer.performWithDelay(1700,getRandomStar, 0)  
 starTimer2 = timer.performWithDelay(2300,getRandomStar, 0)  
 starTimer3 = timer.performWithDelay(2700,getRandomStar, 0)   
end--END startGame()  
   
   
 initStar()  
startGame()  
  
end  
tmr = timer.performWithDelay (75000, star, -1)  

what should I add to stop the timer?

Thanks :slight_smile: [import]uid: 129238 topic_id: 26077 reply_id: 105922[/import]

You create multiple timers multiple times… every 75 seconds you start 3 new timers. I’d cancel the old ones FIRST and make new ones twice as fast rather than two at the same rate, if you intention is invoking twice as often? [import]uid: 52491 topic_id: 26077 reply_id: 105984[/import]

Thanks, is it possible to have some code? :slight_smile: [import]uid: 129238 topic_id: 26077 reply_id: 106276[/import]

I’ve solved myself :slight_smile: thanks anyway [import]uid: 129238 topic_id: 26077 reply_id: 106350[/import]

Glad to hear you got it resolved over the weekend, well done and thank you for marking as resolved :slight_smile: [import]uid: 52491 topic_id: 26077 reply_id: 106622[/import]

you’re welcome :slight_smile: [import]uid: 129238 topic_id: 26077 reply_id: 106644[/import]