How do I call a function with delays but the delays should be halved every time the function is called?

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…

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

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. 

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

[lua]

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)

[/lua]

Dave

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

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. 

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

[lua]

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)

[/lua]

Dave