How to make a DELAY FUNCTION ?

I googled and I found this function and it works:

function delay(delay_time)
    local time_to = os.time() + delay_time
    while os.time() < time_to do end
end

*delay_time is in seconds.  0.6 is 600 miliseconds…

***ORIGINAL MESSAGE

Hi. In my game I need to create a Delay function, because it does not exist in lua natively.

I want this:


code 1

–wait x miliseconds

code 2


I saw timer.performWithDelay but I think it is not what I need because it requires the creation of an extra function and does not hang the code execution…


code 1

* a call to run the extrafunction, x miliseconds in the future

code 3 <-- runs immediately after *

function extrafunction()

   code 2

end


So, how can I make that correctly to perform a real delay (like we see in other programming languages) as I need?

Help me please

Thanks!

I think performWithDelay is the only tool you’ve got to work with – you may need to re-think how the code flows in order to make use of it.  

function code3() ... etc ... end function code2() timer.performWithDelay( 1000, code3 ) end code1 timer.performWithDelay( 1000, code2 )

You’re coming from a more procedural mindset … Corona is really event-driven.  You can use timers (the event is then a timer triggering), or you can look at custom events and addEventListener/dispatchEvent functions.

And what’s more, if you write while function then app will stop stop until while is completed. So if your delay is noticeable as bigger then 100ms, then your method with while will freeze app for this time (no rendering or other code execution - just waiting in while)

I think performWithDelay is the only tool you’ve got to work with – you may need to re-think how the code flows in order to make use of it.  

function code3() ... etc ... end function code2() timer.performWithDelay( 1000, code3 ) end code1 timer.performWithDelay( 1000, code2 )

You’re coming from a more procedural mindset … Corona is really event-driven.  You can use timers (the event is then a timer triggering), or you can look at custom events and addEventListener/dispatchEvent functions.

And what’s more, if you write while function then app will stop stop until while is completed. So if your delay is noticeable as bigger then 100ms, then your method with while will freeze app for this time (no rendering or other code execution - just waiting in while)