Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

timer can't take any parameter
Started by lfk3y Apr 02 2018 09:39 AM

3 replies to this topic
timer delay parameter function

Best Answer roaminggamer , 02 April 2018 - 09:41 AM

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()

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

lfk3y

[GLOBAL: userInfoPane.html]
lfk3y
  • Observer

  • 6 posts
  • Corona SDK

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!



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,404 posts
  • Corona SDK

  Best Answer

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()


Edited by roaminggamer, 02 April 2018 - 09:46 AM.


[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,404 posts
  • Corona SDK

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:

 

Again, welcome and happy developing!


  • Develephant likes this

[TOPIC: post.html]
#4

lfk3y

[GLOBAL: userInfoPane.html]
lfk3y
  • Observer

  • 6 posts
  • Corona SDK

Amazin! Thx




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: timer, delay, parameter, function