Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How do I call a function with delays but the delays should be halved every time the function is called?
Started by sheikhameen123 Apr 26 2019 07:27 AM

4 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

sheikhameen123

[GLOBAL: userInfoPane.html]
sheikhameen123
  • Observer

  • 23 posts
  • Corona SDK

Let me explain.

Let's say I have a function called sup()

I want to call this function with delays...

local timer = timer.performWithDelay(myDelayValue,sup,-1)

Now, I want myDelayValue to be halved after every execution of the function...

For example, if myDelayValue is 40 seconds, after the function is executed once, the next one should be executed after 20 seconds, and the following after 10 seconds, and 5, and 2.5.........



[TOPIC: post.html]
#2

dmglakewood

[GLOBAL: userInfoPane.html]
dmglakewood
  • Contributor

  • 399 posts
  • Corona SDK

local myTimer
local myDelayValue = 5000
local function sup()
	myDelayValue = math.floor(myDelayValue * .5)
	myTimer = timer.performWithDelay(myDelayValue, sup)
end
myTimer = timer.performWithDelay(myDelayValue, sup)


[TOPIC: post.html]
#3

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 939 posts
  • Corona SDK

I would further add to @dmglakewood's sample code that you should assign a handle to the timer(s). Without one, you won't be able to control the timer, i.e. pause or cancel it, should the need arise.


  • dmglakewood likes this

[TOPIC: post.html]
#4

dmglakewood

[GLOBAL: userInfoPane.html]
dmglakewood
  • Contributor

  • 399 posts
  • Corona SDK

I would further add to @dmglakewood's sample code that you should assign a handle to the timer(s). Without one, you won't be able to control the timer, i.e. pause or cancel it, should the need arise.

 

Good point, I edited the sample with that in mind. 



[TOPIC: post.html]
#5

Quantumwave

[GLOBAL: userInfoPane.html]
Quantumwave
  • Contributor

  • 103 posts
  • Corona SDK

Adding a condition to stop the timer would be a good idea, otherwise it gets into an infinite loop.

 

local function sup(myDelayValue)
    if myDelayValue>0 then
        local myTimer = timer.performWithDelay(myDelayValue, function() return sup(myDelayValue) end)
        myDelayValue = math.floor(myDelayValue * 0.5)
    end
end
sup(5000)

 

Dave




[topic_controls]
[/topic_controls]