«timer.performWithDelay» doesn't work properly

Hi guys!

local function test(withArgument)

end

local function test2()             --without argument

end

timer.performWithDelay(5000, test(123))    --will start immidietly

timer.performWithDelay(5000, test2 )         --will start by normal way after 5 sec

Corona version 2018.3326

Can you help me to find a walkaround? I’m stuck on this

Hi,

I’ve never tried to run a timer function with arguments but if that doesn’t work, you’ll just need to set your arguments elsewhere.

I don’t really see the problem.

Maybe you can explain it better.

This means that this is bug.

I have sent a bug-report.

I think there is nothing surprising that a function can be called with arguments

need to set your arguments elsewhere.

I thought about this but I was sure i’m doing wrong, that’s why I have started this topic, now I’m sure this is bug.

It is not a bug…

just do this

timer.performWithDelay(5000, function() test(123) end)

Checking docs there is at the bottom described a way to pass parameters.

It suggest to me that for whatever reason passing arguments in the listener function isnt supported.

Not so sure this is a bug.

https://docs.coronalabs.com/api/library/timer/performWithDelay.html

This is not a bug. Your first call to performWithDelay is calling test(123) and passing the value it returns to the performWithDelay function. Just as if you had done this:

local function test(withArgument) ... end local function test2() --without argument ... end local val = test(123) timer.performWithDelay(5000, val) -- if val is a ref to a function to be called, this is fine timer.performWithDelay(5000, test2 )

Woow-wow-wow, now I have few possibilities to pass params:

timer.performWithDelay(5000, function()
test(123)
end)

local val = test(123) 
timer.performWithDelay(5000, val) – if val is a ref to a function to be called, this is fine
timer.performWithDelay(5000, test2 )

local function onTimer( event ) 

    local params = event.source.params

    print( params.myParam1 )

    print( params.myParam2 )

end

 

local tm = timer.performWithDelay( 1000, onTimer )

tm.params = { myParam1 = “Parameter1”, myParam2 = “Parameter2” }

Thank you guys! 

This piece  «local val = test(123) »

will not work. This will simply execute test func instead of making a ref, isn’t so?

THIS STRANGE LUA LANGUAGE!!! It breaks all programming rules… no, not rules, oh here are these words: stereotypes and and programming habbits. Damn, I can be stuck in few lines of simplest code. Once upon a time I will find them divine, but now this is strange to me )))

This is the way many programming languages work:

variable = someFunction( someParameter )

assigns the return value of someFunction to variable. This never assigns the address of the function to variable. Other languages have different ways to get the reference to a function and Lua keeps it very simple by just using the name of the function.

Rob

Thank you, Rob! I received your answer in letter: 

When you put a function name and include parenthesis, it executes the function immediately and returns the value returned by the function.  When you use the function name without the parenthesis is returns the “address to the function”.

Now this is completely clear and transparent. As I said, the Lua is a strange thing - once you taste it, you cannot forget it. Thanks a lot guys! By the way few other code problems are resolved now )

Hi,

I’ve never tried to run a timer function with arguments but if that doesn’t work, you’ll just need to set your arguments elsewhere.

I don’t really see the problem.

Maybe you can explain it better.

This means that this is bug.

I have sent a bug-report.

I think there is nothing surprising that a function can be called with arguments

need to set your arguments elsewhere.

I thought about this but I was sure i’m doing wrong, that’s why I have started this topic, now I’m sure this is bug.

It is not a bug…

just do this

timer.performWithDelay(5000, function() test(123) end)

Checking docs there is at the bottom described a way to pass parameters.

It suggest to me that for whatever reason passing arguments in the listener function isnt supported.

Not so sure this is a bug.

https://docs.coronalabs.com/api/library/timer/performWithDelay.html

This is not a bug. Your first call to performWithDelay is calling test(123) and passing the value it returns to the performWithDelay function. Just as if you had done this:

local function test(withArgument) ... end local function test2() --without argument ... end local val = test(123) timer.performWithDelay(5000, val) -- if val is a ref to a function to be called, this is fine timer.performWithDelay(5000, test2 )

Woow-wow-wow, now I have few possibilities to pass params:

timer.performWithDelay(5000, function()
test(123)
end)

local val = test(123) 
timer.performWithDelay(5000, val) – if val is a ref to a function to be called, this is fine
timer.performWithDelay(5000, test2 )

local function onTimer( event ) 

    local params = event.source.params

    print( params.myParam1 )

    print( params.myParam2 )

end

 

local tm = timer.performWithDelay( 1000, onTimer )

tm.params = { myParam1 = “Parameter1”, myParam2 = “Parameter2” }

Thank you guys! 

This piece  «local val = test(123) »

will not work. This will simply execute test func instead of making a ref, isn’t so?

THIS STRANGE LUA LANGUAGE!!! It breaks all programming rules… no, not rules, oh here are these words: stereotypes and and programming habbits. Damn, I can be stuck in few lines of simplest code. Once upon a time I will find them divine, but now this is strange to me )))

This is the way many programming languages work:

variable = someFunction( someParameter )

assigns the return value of someFunction to variable. This never assigns the address of the function to variable. Other languages have different ways to get the reference to a function and Lua keeps it very simple by just using the name of the function.

Rob

Thank you, Rob! I received your answer in letter: 

When you put a function name and include parenthesis, it executes the function immediately and returns the value returned by the function.  When you use the function name without the parenthesis is returns the “address to the function”.

Now this is completely clear and transparent. As I said, the Lua is a strange thing - once you taste it, you cannot forget it. Thanks a lot guys! By the way few other code problems are resolved now )