Newbie question, play and pause timer

Hello

I am newbie with Corona and my english is not very good either…

I have a play and a stop button and want to play and stop a timer wich just prints something to the console but I can´t get it to work. Here is my code:

  
local bPlay = display.newText( "Play", 115, 105, "Arial", 30 )  
bPlay:setTextColor( 255, 255, 255 )  
  
local bStop = display.newText( "Stop", 115, 150, "Arial", 30 )  
bStop:setTextColor( 255, 255, 255 )  
  
local t = {}  
 function t:timer( event )  
 local count = event.count  
 print( "Table listener called " .. count .. " time(s)" )  
  
 end  
   
local function play( )  
 print("Play")  
 timer.performWithDelay( 250, t, 0 )  
end  
  
local function stop( )  
 print("Stop")  
 timer.cancel( )  
end  
   
bPlay:addEventListener("tap", play)  
bStop:addEventListener("tap", stop)  
  

It starts OK but when I press stop button I get this error:

  
...  
Table listener called 5 time(s)  
Table listener called 6 time(s)  
Stop  
Runtime error  
 ?:0: attempt to index a nil value  
stack traceback:  
 [C]: ?  
 ?: in function 'cancel'  

Thanks
[import]uid: 9608 topic_id: 5442 reply_id: 305442[/import]

timer.cancel takes a timerID, and you haven’t passed it one
http://developer.anscamobile.com/reference/index/timercancel
[import]uid: 6645 topic_id: 5442 reply_id: 18421[/import]

Hi jmp909, thank you very much, now I think I understand a bit more.

I will paste here the new working code, thanks again!

[code]

local bPlay = display.newText( “Play”, 115, 105, “Arial”, 30 )
bPlay:setTextColor( 255, 255, 255 )

local bStop = display.newText( “Stop”, 115, 150, “Arial”, 30 )
bStop:setTextColor( 255, 255, 255 )

local t = {}
function t:timer( event )
local count = event.count
print( “Table listener called " … count … " time(s)” )
end

local function play()
print(“Play”)
playMusic = timer.performWithDelay( 250, t, 10 )
end

local function stop()
print("Stop ")
timer.cancel(playMusic)
end

bPlay:addEventListener(“tap”, play)
bStop:addEventListener(“tap”, stop)

[/code] [import]uid: 9608 topic_id: 5442 reply_id: 18477[/import]