5

Do you guys have any idea how to detect mobile Chrome sleep event? I tried with:

window.onblur = fnPause;
document.onfocusout = fnPause;
document.body.onfocusout = fnPause;

var hidden, visibilityChange; 
if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support 
    hidden = "hidden";
    visibilityChange = "visibilitychange";
} else if (typeof document.mozHidden !== "undefined") {
    hidden = "mozHidden";
    visibilityChange = "mozvisibilitychange";
} else if (typeof document.msHidden !== "undefined") {
    hidden = "msHidden";
    visibilityChange = "msvisibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
    hidden = "webkitHidden";
    visibilityChange = "webkitvisibilitychange";
}

if (typeof document.addEventListener === "undefined" || 
    typeof document[hidden] === "undefined") {
    // doesn't support event listeners :(
} else {
// Handle page visibility change   
    document.addEventListener(visibilityChange, function() {if (document[hidden]) {fnPause();} else {fnResume();}}, false);
} 

Still fnPause() is never called when I leave the html5 game open and phone goes sleep mode and locks.

This question is not a duplicate of - Is it possible, in JavaScript, to detect when the screen is turned off in the Android & iOS browsers . Two of the solutions I tried above (blur and pagevisibility) without success and the third - using timers - only detects resume event and not sleep.

Community
  • 1
  • 1
Ozdy
  • 51
  • 3
  • Not a duplicate - two of the solutions I tried above (blur and pagevisibility) without success and the third - using timers - only detects resume event and not sleep. – Ozdy Dec 01 '16 at 08:03
  • Even the latest comment of the accepted answer says it doesn't work. – Ozdy Dec 01 '16 at 08:06
  • well... it doesn't have to work. Its just that same question was asked and discussed before. It is highly possible, that there is no working solution for this issue, since browsers AFAIK don't provide such a tight integration with the underlying platform. If you'd opt for hybrid app for your game - that is another story. – Vladimir M Dec 01 '16 at 08:14
  • There are html5 games that properly detect that, so there is a working solution. – Ozdy Dec 01 '16 at 08:39
  • Also, I don't want my question marked a duplicate with another with a wrongly accepted answer, that way nobody would care to answer me :) – Ozdy Dec 01 '16 at 08:45
  • removed the flag per your request. – Vladimir M Dec 01 '16 at 09:29

0 Answers0