[Sorry for the wrong place, but I must do it] Problem with timer.pause and timer.resume

 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:

tgxajEf.jpg

 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:

  PivG4sh.jpg

 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)

 I find out another information: if you create any time when pause or resume, this bug will disapear.

 Like that:

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) local fakeTimer = timer.performWithDelay(t,function() print("fake timer for test, time:",getTimer()) end) end

Just to make sure I understand the problem.  If you set a timer for 5 seconds, pause it after 1 second, resume it after 3 seconds and you would expect it to delay the timer by 3 seconds and for it to really fire at 8 seconds, vs. pause a reoccuring timer from firing, but when resumed it would continue on it’s set pace of every 5 seconds.

Is that a problem?

  Sorry, I’m not good at English, so I will make it clear.

 As you said, I expect it to fire at the eighth second, but it still fires at the fifth second.

But if I pause it more than five seconds ( the timer duration ), it will fire at the right time. Or I can create any timer when I resume it ( as my second post ), it will go right, too.

Engineering has tested this and it’s working as expected.  If you pause the timer the time is added and if you pause a 5 second timer for 3 seconds, it fires at 8 seconds.

Try this code:

-- Called when the timer elapses. local function onTimerElapsed(event)    print(os.date() .. ": Elapsed") end -- Start the timer. local myTimer = timer.performWithDelay(5000, onTimerElapsed, -1) local isTimerRunning = true print(os.date() .. ": Started") -- Pauses/resumes the timer when the screen is tapped. local function onTap(event)    if isTimerRunning then       timer.pause(myTimer)       print(os.date() .. ": Paused")    else       timer.resume(myTimer)       print(os.date() .. ": Resumed")    end    isTimerRunning = not isTimerRunning end Runtime:addEventListener("tap", onTap)

Rob

 Hi Rob.

 I have just run your code. Here’s my log if I paused timer for 2 seconds

rGXjqgJ.jpg

 As you see. Timer started at 42:10, paused 2 seconds from 11->13, so it must fire at 13 + 4 ( remain time ) = 17.

But it fired at 15. So that is the bug, or I misunderstand Corona Timer Pause’s meaning?

 But if you pause more time and make the timer miss its FireTime, everything will be fine. Here’s another log if I pause timer long enough to make it miss first-time-fire:

JDeZUlZ.jpg

 Started at 44, run for one second, paused 4 seconds from 45->49, resumed at 49 and wait 4 seconds to fired at 53.

 Hi Rob.

 Did you check it again?

Engineering as seen the issue.  I’m not sure of the cycle when it will be worked on.  I understand there is a fix waiting for review, but I might have mis-read that email.

Rob

 Thanks.

 Wait for the fix.

 I’ve been checking daily builds regularly, but haven’t seen anything relating to this.

 How could I check if it is fixed? I’m still using my custom timer to avoid this error, but it’s just a little complex.

 Thanks.

Hi khanh.dq,

We’ve been pretty busy here in the office. I have a fix for the issue and will post here to let you know when it is expected to be in a daily build. You can also check the changelogs available on the daily build page.

Regards,

Michael

 Thank Michael.

 Have a nice week :slight_smile:

My apologies for the long delay. This should be fixed in tomorrow’s daily build.

 Hi Michael, glad to hear that. A little sad that it didn’t come with the lastest Public Release.

 I will check tomorrow’s daily build and see what happends.

 Thanks,

 Glad to see it work as it should :slight_smile:

 Thank Micheal and Rob

 I find out another information: if you create any time when pause or resume, this bug will disapear.

 Like that:

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) local fakeTimer = timer.performWithDelay(t,function() print("fake timer for test, time:",getTimer()) end) end

Just to make sure I understand the problem.  If you set a timer for 5 seconds, pause it after 1 second, resume it after 3 seconds and you would expect it to delay the timer by 3 seconds and for it to really fire at 8 seconds, vs. pause a reoccuring timer from firing, but when resumed it would continue on it’s set pace of every 5 seconds.

Is that a problem?

  Sorry, I’m not good at English, so I will make it clear.

 As you said, I expect it to fire at the eighth second, but it still fires at the fifth second.

But if I pause it more than five seconds ( the timer duration ), it will fire at the right time. Or I can create any timer when I resume it ( as my second post ), it will go right, too.

Engineering has tested this and it’s working as expected.  If you pause the timer the time is added and if you pause a 5 second timer for 3 seconds, it fires at 8 seconds.

Try this code:

-- Called when the timer elapses. local function onTimerElapsed(event)    print(os.date() .. ": Elapsed") end -- Start the timer. local myTimer = timer.performWithDelay(5000, onTimerElapsed, -1) local isTimerRunning = true print(os.date() .. ": Started") -- Pauses/resumes the timer when the screen is tapped. local function onTap(event)    if isTimerRunning then       timer.pause(myTimer)       print(os.date() .. ": Paused")    else       timer.resume(myTimer)       print(os.date() .. ": Resumed")    end    isTimerRunning = not isTimerRunning end Runtime:addEventListener("tap", onTap)

Rob

 Hi Rob.

 I have just run your code. Here’s my log if I paused timer for 2 seconds

rGXjqgJ.jpg

 As you see. Timer started at 42:10, paused 2 seconds from 11->13, so it must fire at 13 + 4 ( remain time ) = 17.

But it fired at 15. So that is the bug, or I misunderstand Corona Timer Pause’s meaning?

 But if you pause more time and make the timer miss its FireTime, everything will be fine. Here’s another log if I pause timer long enough to make it miss first-time-fire:

JDeZUlZ.jpg

 Started at 44, run for one second, paused 4 seconds from 45->49, resumed at 49 and wait 4 seconds to fired at 53.