HTML5 games with timers

Hello everyone,

I was wondering how to deal in a quizz game where you have, for example, 20 seconds to give an answer.

Leaving the html page will suspend the application and all timers, and the player can come back long after the 20 seconds and give an answer.

Is there a work around ?

Use the clock instead?

https://docs.coronalabs.com/api/library/os/clock.html

Hi RoamingGamer,

os.clock () is also suspended : when the html app resumes os.clock () resumes but does not reflect the real time elapsed.

Hi,

You can use the system events to check for the various states (start, suspend, resume, etc.) using the “system” runtime event listener.

 

Store the timer value on suspend and on resume check to see if over 20 seconds has elapsed and immediately move on, or whatever your logic is after a question timeout.

local last\_time = os.time() local function onSystemEvent( e ) if e.type == "applicationSuspend" then --HTML5 app is being paused last\_time = os.time() elseif e.type == "applicationResume" then --HTML5 app has started again. local elasped = os.difftime(os.time(), last\_time) print(elasped) --\> seconds elapsed end end Runtime:addEventListener("system", onSystemEvent)

-dev

You mean the clock doesn’t advance?

See @dev’s advice.

I’m saying check the clock either regularly on a timer or after you resume.

I’m not saying you can make something happen while the window is suspended.  :slight_smile:

Try this example:

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/04/html5SuspendedTimer.zip

Use the clock instead?

https://docs.coronalabs.com/api/library/os/clock.html

Hi RoamingGamer,

os.clock () is also suspended : when the html app resumes os.clock () resumes but does not reflect the real time elapsed.

Hi,

You can use the system events to check for the various states (start, suspend, resume, etc.) using the “system” runtime event listener.

 

Store the timer value on suspend and on resume check to see if over 20 seconds has elapsed and immediately move on, or whatever your logic is after a question timeout.

local last\_time = os.time() local function onSystemEvent( e ) if e.type == "applicationSuspend" then --HTML5 app is being paused last\_time = os.time() elseif e.type == "applicationResume" then --HTML5 app has started again. local elasped = os.difftime(os.time(), last\_time) print(elasped) --\> seconds elapsed end end Runtime:addEventListener("system", onSystemEvent)

-dev

You mean the clock doesn’t advance?

See @dev’s advice.

I’m saying check the clock either regularly on a timer or after you resume.

I’m not saying you can make something happen while the window is suspended.  :slight_smile:

Try this example:

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/04/html5SuspendedTimer.zip