lfk3y
April 2, 2018, 5:39pm
1
Wonderful day!
WHY can’t the timer.performWithDelay take any parameter?
For example:
function writeText(text) print(text) end timer.performWithDelay(3000, writeText("thisIsSomeText"), 0)
That doesn’t work. The timer completly ignores the delay and is doing it instanlty.
It only works if i do
function writeText() print "text" end timer.performWithDelay(3000, writeText, 0)
so without the brackets, which is pretty annyoing bc this way i have to make a new function every time?
What is wrong with the code - why is this not working? How to do this else?
Thanks!
You need to use a closure.
(This is clearly described in the docs: https://docs.coronalabs.com/api/library/timer/performWithDelay.html#passing-parameters-1) )
Your code is a direct call to the function writeText():
timer.performWithDelay(3000, writeText("thisIsSomeText"), 0)
This is the right way to do it:
timer.performWithDelay(3000, function() writeText("thisIsSomeText") end, 0)
Give the docs a quick re-read and you’ll see this:
https://docs.coronalabs.com/api/library/timer/performWithDelay.html
timer.performWithDelay( delay, listener [, iterations] )
Listener is the function to call. i.e. It has to be a reference to a function.
In your code you’re calling the function an passing what it returns to timer.performWithDelay()
Here is the API docs page: https://docs.coronalabs.com/api/
I reference this a dozen times a day, so I keep on my browser bookmarks links bar for easy access.
Welcome to the community by the way.
Note: While I may seem gruff, I do like to help folks a lot, but I’m also not afraid to tell folks when they could have looked it up. Corona has absolutely stellar docs and there are tons of examples, here and about:
For your list later:
Some Critical Corona Pages:
My Stuff
Again, welcome and happy developing!
You need to use a closure.
(This is clearly described in the docs: https://docs.coronalabs.com/api/library/timer/performWithDelay.html#passing-parameters-1) )
Your code is a direct call to the function writeText():
timer.performWithDelay(3000, writeText("thisIsSomeText"), 0)
This is the right way to do it:
timer.performWithDelay(3000, function() writeText("thisIsSomeText") end, 0)
Give the docs a quick re-read and you’ll see this:
https://docs.coronalabs.com/api/library/timer/performWithDelay.html
timer.performWithDelay( delay, listener [, iterations] )
Listener is the function to call. i.e. It has to be a reference to a function.
In your code you’re calling the function an passing what it returns to timer.performWithDelay()
Here is the API docs page: https://docs.coronalabs.com/api/
I reference this a dozen times a day, so I keep on my browser bookmarks links bar for easy access.
Welcome to the community by the way.
Note: While I may seem gruff, I do like to help folks a lot, but I’m also not afraid to tell folks when they could have looked it up. Corona has absolutely stellar docs and there are tons of examples, here and about:
For your list later:
Some Critical Corona Pages:
My Stuff
Again, welcome and happy developing!