timer.performWithDelay not working!?

Hi,

timer.performWithDelay seems to be not working for me.

Consider the following program that consists of 3 lines

main.lua

print("now") timer.performWithDelay(5000, print("5 seconds later")) timer.performWithDelay(10000, print("10 seconds later"))

When I launch the program, I expect “now” to be printed in the console immediately, and “5 seconds later” to be printed 5 seconds later, etc.

However, they all are printed simultaneously!

What am I doing wrong?

I am using Corona Version 2016.2949 (2016.9.15) if it’s of any relevance.

Thanks in advance!

The second parameter to the performWithDelay function is an actual function. What you’re doing is calling the print function and passing whatever it returns to the performWithDelay parameter list.

What you want to do is this…

print("now") timer.performWithDelay(5000, function() print("5 seconds later") end) timer.performWithDelay(10000, function() print("10 seconds later") end)

Just to expand on the above answer, remember that functions in Lua are first class citizens, so you reference them just like variables.

Also, you’ll need to remember that the function being passed into the second parameter of performWithDelay takes no arguments, so you can’t move the ‘5’ into the ‘function()’ and expect it to work - it won’t.

If you want to parameterise the function being called in the second parameter of performWithDelay you would do something like this…

local function when( time ) print(time.." seconds later") end print("now") timer.performWithDelay(5000, function() when(5) end) timer.performWithDelay(10000, function() when(10) end)

Thank you horacebury! Your solution worked perfectly!

I come from a Java background, so this “functions are first class citizens” business is all new to me :stuck_out_tongue:

The second parameter to the performWithDelay function is an actual function. What you’re doing is calling the print function and passing whatever it returns to the performWithDelay parameter list.

What you want to do is this…

print("now") timer.performWithDelay(5000, function() print("5 seconds later") end) timer.performWithDelay(10000, function() print("10 seconds later") end)

Just to expand on the above answer, remember that functions in Lua are first class citizens, so you reference them just like variables.

Also, you’ll need to remember that the function being passed into the second parameter of performWithDelay takes no arguments, so you can’t move the ‘5’ into the ‘function()’ and expect it to work - it won’t.

If you want to parameterise the function being called in the second parameter of performWithDelay you would do something like this…

local function when( time ) print(time.." seconds later") end print("now") timer.performWithDelay(5000, function() when(5) end) timer.performWithDelay(10000, function() when(10) end)

Thank you horacebury! Your solution worked perfectly!

I come from a Java background, so this “functions are first class citizens” business is all new to me :stuck_out_tongue: