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.)
onblur
andonfocus
events, given that Android typically goes to some sort of lock screen? I would think turning it off might fireonblur
, and at the very least waking it up would fireonblur
(if turning it off doesn't) because Android brings up the lock screen, and then after getting past the lock screen,onfocus
would fire since you're transitioning from the lock screen back to the active app. I'm not an Android expert, but figured I'd throw that thought out if you hadn't checked. – Crawler