timer.performWithDelay inaccurate time with cooldown timer

I have a cooldown timer which I made by creating a small slice and displaying it 360 times to make a circle.  

To show the cooldown is being reset, I remove a slice of the circle.  

It works fine, except that the time I am expecting it to take to run the code to remove one slice is taking longer than expected, and is not consistent…  

Here is my block of code to remove the slice:

local the\_time = system.getTimer() local slice\_counter = 1 local function removeSlice() print("time since last ran: " .. (system.getTimer() - the\_time)) the\_time = system.getTimer() display.remove(cd\_slices[slice\_counter]) cd\_slice[slice\_counter] = nil if(slice\_counter == 360) then cdOver() end slice\_counter = slice\_counter + 1 end print("cd\_length: " .. cd\_length) cd\_timer = timer.performWithDelay(cd\_length, removeSlice, 360)

I would expect the time taken between each time it removes a slice to be the same, and to be equal to cd_length. 

But it is not… 

cd_length is 5.6, but

time since last ran is all over the place, ranging from  9 to 21 instead of 5.6.  

Am I doing something wrong or misunderstanding how timer.performWithDelay works??

By default, timed functions are only triggered on a frame tick, so the only granularity you can get is 30 or 60 fps. 

However, they did recently add an undocumented flag that you can set - timer.allowIterationsWithinFrame = true

I’ve not used this personally so whether it will do what you want I’m not sure. However, whether you can fire events between frames or not, you can’t update any of the visuals any faster than 30/60 fps. You’ll either have to have your cool down timer take exactly 360 frames, or remove more than one slice per frame.

So I changed the code to remove 10 slices instead of just 1.  So in theory I would need the cooldown to be at least 0.6 seconds right?

360 slices / 10 means it needs 36 iterations.  Game is set to 60 fps so  36/60 gives me 0.6 seconds.

I set my cooldown to be 2 seconds(which should be plenty of time) so I end up with cooldown_length of 54.  

However, I still see the time taking longer than 54.  It ranges from taking 55 to 74.  

Did I miscalculate something there?

Frame delta times aren’t consistent. Ideally they would always be 16.66ms apart but it will generally be somewhere north or south of that - if you Google there have been a number of discussions on here about that.

So I guess you’ve got to adjust the number of slices to remove based on the time elapsed since the last frame compared to the exact 1/60th.

Gotcha.   It makes sense what is happening now.  Thank you!