The Screen Goes Dark Mid-Round: Wake Lock on Android Browsers
Put the phone on the table, keep both hands free, and the display dims mid-round. Stock Android ships with a 30-second screen timeout, so a web game that needs a tap once a minute spends more time fighting the lock scree
Put the phone on the table, keep both hands free, and the display dims mid-round. Stock Android ships with a 30-second screen timeout, so a web game that needs a tap once a minute spends more time fighting the lock screen than the level.
navigator.wakeLock.request('screen') exists for exactly this. It resolves to a sentinel object, only works in a secure context, and is refused when the page is not visible or the battery is nearly empty. The lock is also released when the tab drops to the background, so it has to be requested again inside a visibilitychange handler. Firefox on Android does not implement it at all, so feature detection is mandatory.
Among the H5 entries I keep on a home screen, hiwin handles this cheaply: a muted looping background clip keeps the display awake where the API is missing, at some battery cost. bigwinner h5 shows a "tap to keep the screen on" strip instead, honest but tedious every few minutes.
Three sat side by side on a mid-range phone with the timeout pinned to 30 seconds. With s13 the screen stayed lit for the whole session, while v87 went dark at almost exactly the 30-second mark, because its lock was requested before the page had focus. rvip2 fell in between: lit while I was touching the screen, dark the moment I stopped.
The fix is to request the lock on touchstart as well as on load, and to release the sentinel when the round ends. Two habits are worth keeping: log whether wakeLock.request resolved or rejected, because a rejected promise is otherwise silent, and add a visible hint for browsers without the API, since a player who cannot see the screen will assume the game froze, not the phone.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes ā full credit and traffic to the original publisher.