Need to make a gradually speeding up loop. [SOLVED]

Hi! I’m making my first project with corona right now. I have everything working so far, but I just need to find out one thing: how can I make a slowly speeding up loop? I just need any kind of loop that executes a function , and it keeps happening faster and faster.

For instance, it will execute the function every 3000 ms. Every time it executes, though, it goes down by 10 ms.

I’ve made a sleep function from code online but all that does is freezes the program, which I don’t want. I’ve also tried timer.performWithDelay using “enterFrame” but that instantly reaches huge speeds.

Can anyone help me? 

This code prints out using timer.performWithDelay() starting at every 1000ms delay and dropping to 100 and exiting at 0:

local tm = {count=0} local function handleit(event) print(event.name..event.time) tm.count = tm.count + 1 if tm.count == 10 then print"timer reset" tm.count = 0 tm["timer"]= nil tm:set\_timer() end end function tm:set\_timer() if self.speed == nil then self.speed = 1000 print("timer start speed 1000") elseif self.speed == 0 then print "done" return else self.speed = self.speed - 100 print("new timer speed: "..self.speed) end self["timer"] = timer.performWithDelay(self.speed, handleit, 10) end tm:set\_timer()

Edit: 

I cleaned up the code a little.

I made sure to set the expired timer table entry to nil before creating a new one.  

I think without that there might have been a memory leak.  

Here is another method. This one uses two timers. 

The first timer called print_timer acts as the loop and prints the event.name and event.time to the console. 

The second timer called speed_timer is used to gradually decrease the delay in print_timer causing it to fire events faster. 

local print\_timer = nil local speed\_timer = {speed = 1010} local function handle\_print\_timer(event) print(event.name.." "..event.time) end local function handle\_speed\_timer(event) speed\_timer.speed = speed\_timer.speed - 10 if speed\_timer.speed \<=0 then timer.cancel(speed\_timer["timer"]) speed\_timer["timer"] = nil speed\_timer.speed = 1 print("Maximum speed set") end if print\_timer ~= nil then timer.cancel(print\_timer) print\_timer = nil end print\_timer = timer.performWithDelay(speed\_timer.speed, handle\_print\_timer, 1000) print("Speed changed to: "..speed\_timer.speed) end speed\_timer["timer"] = timer.performWithDelay(2000, handle\_speed\_timer, 1000 )

In the code I have set a maximum iterations for each timer. This is only so it didn’t run constantly during testing.   

The speed timer is set to change the speed every 2 seconds. 

The print timer begins printing events at a rate of 1 per second. So you will see two printed timer events between speed changes until the delay drops to about 650. Then the print timer will start printing 3 events for every speed change. And then obviously this grows as the delay drops.  

When the delay drops below 0, I set the print timer speed to 1 and remove the speed timer.  This will give you maximum speed from the timer. 

Not wanting to miss out, this is my 2-cents.

local resetOnMin = true local maxPeriod = 5000 local minPeriod = 60 local periodDelta = 100 local lastT = system.getTimer() local curPeriod = maxPeriod local function work() print("Period: " .. tostring(curPeriod) .. " @ " .. tostring(system.getTimer()) ) end local function enterFrame() local curT = system.getTimer() local dt = curT - lastT -- if( dt \< curPeriod ) then return end lastT = curT -- work() -- curPeriod = curPeriod - periodDelta if( curPeriod \< minPeriod ) then if( resetOnMin ) then curPeriod = maxPeriod else curPeriod = minPeriod end end end; Runtime:addEventListener( "enterFrame", enterFrame )

And a timer variant:
 

local proxy = {} proxy.resetOnMin = true proxy.maxPeriod = 5000 proxy.minPeriod = 60 proxy.periodDelta = 100 proxy.curPeriod = maxPeriod local function work() print("Period: " .. tostring(proxy.curPeriod) .. " @ " .. tostring(system.getTimer()) ) end function proxy.timer( self ) work() -- self.curPeriod = self.curPeriod - self.periodDelta -- if( self.curPeriod \< self.minPeriod ) then if( self.resetOnMin ) then self.curPeriod = self.maxPeriod else self.curPeriod = self.minPeriod end end timer.performWithDelay( self.curPeriod, self ) end; timer.performWithDelay( proxy.curPeriod, proxy )

Thank you both so much for the help! I’ve been waiting a month to get this project done, I had a great idea and I couldn’t finish this without the loop. 

This code prints out using timer.performWithDelay() starting at every 1000ms delay and dropping to 100 and exiting at 0:

local tm = {count=0} local function handleit(event) print(event.name..event.time) tm.count = tm.count + 1 if tm.count == 10 then print"timer reset" tm.count = 0 tm["timer"]= nil tm:set\_timer() end end function tm:set\_timer() if self.speed == nil then self.speed = 1000 print("timer start speed 1000") elseif self.speed == 0 then print "done" return else self.speed = self.speed - 100 print("new timer speed: "..self.speed) end self["timer"] = timer.performWithDelay(self.speed, handleit, 10) end tm:set\_timer()

Edit: 

I cleaned up the code a little.

I made sure to set the expired timer table entry to nil before creating a new one.  

I think without that there might have been a memory leak.  

Here is another method. This one uses two timers. 

The first timer called print_timer acts as the loop and prints the event.name and event.time to the console. 

The second timer called speed_timer is used to gradually decrease the delay in print_timer causing it to fire events faster. 

local print\_timer = nil local speed\_timer = {speed = 1010} local function handle\_print\_timer(event) print(event.name.." "..event.time) end local function handle\_speed\_timer(event) speed\_timer.speed = speed\_timer.speed - 10 if speed\_timer.speed \<=0 then timer.cancel(speed\_timer["timer"]) speed\_timer["timer"] = nil speed\_timer.speed = 1 print("Maximum speed set") end if print\_timer ~= nil then timer.cancel(print\_timer) print\_timer = nil end print\_timer = timer.performWithDelay(speed\_timer.speed, handle\_print\_timer, 1000) print("Speed changed to: "..speed\_timer.speed) end speed\_timer["timer"] = timer.performWithDelay(2000, handle\_speed\_timer, 1000 )

In the code I have set a maximum iterations for each timer. This is only so it didn’t run constantly during testing.   

The speed timer is set to change the speed every 2 seconds. 

The print timer begins printing events at a rate of 1 per second. So you will see two printed timer events between speed changes until the delay drops to about 650. Then the print timer will start printing 3 events for every speed change. And then obviously this grows as the delay drops.  

When the delay drops below 0, I set the print timer speed to 1 and remove the speed timer.  This will give you maximum speed from the timer. 

Not wanting to miss out, this is my 2-cents.

local resetOnMin = true local maxPeriod = 5000 local minPeriod = 60 local periodDelta = 100 local lastT = system.getTimer() local curPeriod = maxPeriod local function work() print("Period: " .. tostring(curPeriod) .. " @ " .. tostring(system.getTimer()) ) end local function enterFrame() local curT = system.getTimer() local dt = curT - lastT -- if( dt \< curPeriod ) then return end lastT = curT -- work() -- curPeriod = curPeriod - periodDelta if( curPeriod \< minPeriod ) then if( resetOnMin ) then curPeriod = maxPeriod else curPeriod = minPeriod end end end; Runtime:addEventListener( "enterFrame", enterFrame )

And a timer variant:
 

local proxy = {} proxy.resetOnMin = true proxy.maxPeriod = 5000 proxy.minPeriod = 60 proxy.periodDelta = 100 proxy.curPeriod = maxPeriod local function work() print("Period: " .. tostring(proxy.curPeriod) .. " @ " .. tostring(system.getTimer()) ) end function proxy.timer( self ) work() -- self.curPeriod = self.curPeriod - self.periodDelta -- if( self.curPeriod \< self.minPeriod ) then if( self.resetOnMin ) then self.curPeriod = self.maxPeriod else self.curPeriod = self.minPeriod end end timer.performWithDelay( self.curPeriod, self ) end; timer.performWithDelay( proxy.curPeriod, proxy )

Thank you both so much for the help! I’ve been waiting a month to get this project done, I had a great idea and I couldn’t finish this without the loop.