Accuracy issues with timer.performWithDelay

I have a function which is supposed to play a sound at an interval that gets shorter every time the function is called. It works by calling itself through timer.performWithDelay() using a value that decreases automatically.

  
local timerDelay = 250  
local timerDecrease = 10  
  
local time = system.getTimer()  
  
local function pewLooper()  
 if timerDelay \> 0 then  
  
 timerDelay = timerDelay - decrease;  
  
 local now = system.getTimer()  
 local elapsed = now - time  
  
 playPew()  
  
 time = now  
  
 print('elapsed = ' .. elapsed)  
 print('timerDelay = ' .. timerDelay)  
  
 timer.performWithDelay( timerDelay, pewLooper )  
  
 end  
end  
  
pewLooper()  
  

When I run the function, timerDelay decreases as intended but the elapsed delay only decreases every three or four times the function is called.

Here’s the debug text:

  
timerDelay = 240  
elapsed = 263  
timerDelay = 230  
elapsed = 231  
timerDelay = 220  
elapsed = 231  
timerDelay = 210  
elapsed = 231  
timerDelay = 200  
elapsed = 231  
timerDelay = 190  
elapsed = 198  
timerDelay = 180  
elapsed = 198  
timerDelay = 170  
elapsed = 198  
timerDelay = 160  
elapsed = 165  
timerDelay = 150  
elapsed = 165  
timerDelay = 140  
elapsed = 165  
timerDelay = 130  
elapsed = 132  
timerDelay = 120  
elapsed = 132  
timerDelay = 110  
elapsed = 132  
timerDelay = 100  
elapsed = 132  
timerDelay = 90  
elapsed = 99  
timerDelay = 80  
elapsed = 99  
timerDelay = 70  
elapsed = 99  
timerDelay = 60  
elapsed = 66  
timerDelay = 50  
elapsed = 66  
timerDelay = 40  
elapsed = 66  
timerDelay = 30  
elapsed = 33  
timerDelay = 20  
elapsed = 33  
timerDelay = 10  
elapsed = 33  
timerDelay = 0  
  

What’s happening is that the elapsed delay doesn’t change until (elapsed - timerDelay) > 33. This is true regardless of what timerDelay and timerDecrease are set to.

I know that the elapsed numbers are correct because I can actually hear the interval between pew making big jumps instead of increasing smoothly.

This is really starting to drive me nuts, so thanks in advance for any suggestions… [import]uid: 10489 topic_id: 6532 reply_id: 306532[/import]

you can’t get accurate timing [import]uid: 6645 topic_id: 6532 reply_id: 22851[/import]

[lua]local clock=os.clock
local lastTime = clock()

local function check()

print(clock()-lastTime)
lastTime=clock()
end

timer.performWithDelay(1000,check,-1)[/lua]

it’s never 1.0 seconds [import]uid: 6645 topic_id: 6532 reply_id: 22855[/import]

I do remember reading that it’s impossible to get 100% accurate timing, but I still think it’s strange that elapsed time consistently decreases in increments of 33.

Whatever the issue is, it’s not a deal breaker. I’ll just rewrite my code using enterframe. [import]uid: 10489 topic_id: 6532 reply_id: 22995[/import]

well 33.3 = 1000/30

try setting your framerate to 60 and see if you get 16 or 17 instead?

just a thought. [import]uid: 6645 topic_id: 6532 reply_id: 23001[/import]

Thanks, jmp, that actually makes perfect sense. I’ll try your idea when I get home. [import]uid: 10489 topic_id: 6532 reply_id: 23038[/import]

Just tried it at 60fps and the decrease amount went from 33 to 16 like you guessed.

Thanks again for your help – now that I understand what’s happening I can keep moving on the project.

[import]uid: 10489 topic_id: 6532 reply_id: 23130[/import]