Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
109
rated 0 times [  114] [ 5]  / answers: 1 / hits: 26472  / 11 Years ago, thu, april 11, 2013, 12:00:00

I was tracking down some ridiculously high load times that my app's javascript reported, and found that Android (and iOS) pause some JavaScript execution when the window is in the background or the display is off.



On Android, I found that I could use the window.onfocus and onblur events to detect when the app was switching to the background (and js execution would soon be paused, at least for new scripts), but I can't find a way to detect when the screen is turned on or off. Is this possible?



(On Safari, I had similar results except that onfocus and onblur didn't fire reliably.)


More From » android

 Answers
31

There is few options to check it:




  1. Using Visibility API


  2. Using focus and blur events to detect browser tab visibility:





window.addEventListener(focus, handleBrowserState.bind(context, true));
window.addEventListener(blur, handleBrowserState.bind(context, false));

function handleBrowserState(isActive){
// do something
}



  1. Using timers, as mentioned above


[#78960] Wednesday, April 10, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
terrence

Total Points: 120
Total Questions: 115
Total Answers: 87

Location: England
Member since Fri, May 22, 2020
4 Years ago
terrence questions
Sat, Jun 5, 21, 00:00, 3 Years ago
Wed, Jun 17, 20, 00:00, 4 Years ago
;