how to delay

how do i make a delay on a button

This code will add an event listener to “myButton” and after one second (1000 milliseconds) “myFunction()” will be called.

myButton:addEventListener( "tap", function() timer.performWithDelay( 1000, myFunction ) end )

can you give me the code with a button to because i can’t seem to get it to work

--------------------------------------------------------------------------------- -- -- main.lua -- --------------------------------------------------------------------------------- -- hide the status bar display.setStatusBar( display.HiddenStatusBar ) local function onTapButton( event ) print("Immediately we are in 'onTapButton'!") local function delayedPrint() print("But I come a little bit later ...") end timer.performWithDelay( 1000, delayedPrint ) end local myButton = display.newRect( 100, 100, 100, 100 ) myButton:addEventListener( "tap", onTapButton )

i need to prevent spam of a button would this work

My previous code will not work for that. It will only delay before the other function gets called.

This code checks if the button is being pressed and stores a time value. If the last_successful_tap was three seconds ago it will proceed, otherwise it will print “Stop spamming!” in the console:

local last\_successful\_tap = nil local function onTapButton() if last\_successful\_tap == nil then last\_successful\_tap = os.time() end if os.time() - last\_successful\_tap \> 3 then last\_successful\_tap = os.time() print("Ok, let's proceed!") else print("Stop spamming!") end end local myButton = display.newRect( 100, 100, 100, 100 ) myButton:addEventListener( "tap", onTapButton )

Also you could include how many times the button have been pressed by the user so for example the user can only tap the button every 2 seconds but also only 5 times per second or something like that.

thank you so much

It really helps to have a better description of what you want to do. “I want a delay on a button” could be for any reason and provoke a variety of responses. 

Rob

This code will add an event listener to “myButton” and after one second (1000 milliseconds) “myFunction()” will be called.

myButton:addEventListener( "tap", function() timer.performWithDelay( 1000, myFunction ) end )

can you give me the code with a button to because i can’t seem to get it to work

--------------------------------------------------------------------------------- -- -- main.lua -- --------------------------------------------------------------------------------- -- hide the status bar display.setStatusBar( display.HiddenStatusBar ) local function onTapButton( event ) print("Immediately we are in 'onTapButton'!") local function delayedPrint() print("But I come a little bit later ...") end timer.performWithDelay( 1000, delayedPrint ) end local myButton = display.newRect( 100, 100, 100, 100 ) myButton:addEventListener( "tap", onTapButton )

i need to prevent spam of a button would this work

My previous code will not work for that. It will only delay before the other function gets called.

This code checks if the button is being pressed and stores a time value. If the last_successful_tap was three seconds ago it will proceed, otherwise it will print “Stop spamming!” in the console:

local last\_successful\_tap = nil local function onTapButton() if last\_successful\_tap == nil then last\_successful\_tap = os.time() end if os.time() - last\_successful\_tap \> 3 then last\_successful\_tap = os.time() print("Ok, let's proceed!") else print("Stop spamming!") end end local myButton = display.newRect( 100, 100, 100, 100 ) myButton:addEventListener( "tap", onTapButton )

Also you could include how many times the button have been pressed by the user so for example the user can only tap the button every 2 seconds but also only 5 times per second or something like that.

thank you so much

It really helps to have a better description of what you want to do. “I want a delay on a button” could be for any reason and provoke a variety of responses. 

Rob