At first, I sorry that I must post a topic about timer here. I see two important bugs about timer in “Other” group but no one answers them.
Let see I have a simple code with timer:
local startPauseTime = 0 local endPauseTime = 0 local totalPauseTime = 0 local lastForceTime = 0 local currentForceTime = 0 local getTimer = system.getTimer local testTimer testTimer = timer.performWithDelay(5000, function () local currentTime = getTimer() print("---Run timer ------------------------------") print(" + last run time: =", lastForceTime) print(" + current time: =", currentTime) print(" + so delta time =", currentTime - lastForceTime - totalPauseTime) totalPauseTime = 0 lastForceTime = currentTime end,1) local pause = false local function pauseTestTimer() local t = timer.pause(testTimer) startPauseTime = getTimer() print ("pause time at : ",startPauseTime," remaining time: ",t) end local function resumeTestTimer() local t = timer.resume(testTimer) endPauseTime = getTimer() print("resume time at :" ,endPauseTime," remaining time: ",t,"suggest time run: ",t + endPauseTime) print("calculate on resume:") print(" + current total Time Pause: ",totalPauseTime) print(" + delta time pause: ",endPauseTime - startPauseTime) totalPauseTime = totalPauseTime + (endPauseTime - startPauseTime) print(" + new total Time Pause: ",totalPauseTime) end local bg = display.newRect( -100,-100, display.contentWidth, display.contentHeight ) bg.x, bg.y = display.contentWidth / 2, display.contentHeight / 2 bg:setFillColor(0.3,0.3,0.3) bg:toBack() bg:addEventListener("touch", function(event) if (event.phase=="began") then if (pause==true) then pause = false resumeTestTimer() return true else pause = true pauseTestTimer() return true end elseif (event.phase=="ended") then return false end end)
Just put it in main.lua, run it, click at time 1000ms to pause timer, wait for ~3000ms and click again to resume it. And you can see the timer will run at a wrong time.
I post here a log of me running it:
You can see it. The timer pause seems it doesn’t effect the timer. It still run at 5000ms ( so its time wait is only 1600ms).
But if when I pause the timer, I wait more time than the timer require, it will run perfectly. Here is the log when I wait ~6000ms then resume:
So is that a critical bug?
One more information: if a create two timers like that, everything goes fine
timer.performWithDelay(1000,pauseTestTimer) timer.performWithDelay(3000,resumeTestTimer)