Javascript pauses ipad when changing tabs: is there any way to know when it will return?

I have the same problem as ios 5 pauses javascript when the tab is inactive .

My question is whether you can notice when I get back to the pause.

onfocus and onblur events do not work on the pause tab.

Thanks, Luke

the code:

<html xmlns="http://www.w3.org/1999/xhtml"> <head> <script src="http://code.jquery.com/jquery-1.7.1.min.js" ></script> <script type="text/javascript"> window.onblur = function () { console.log("blur"); $("#whatevent").append("blur<br/>"); } window.onfocus = function () { console.log("focus"); $("#whatevent").append("focus<br/>"); } window.onunload = function () { console.log("unload"); $("#whatevent").append("unload<br/>"); } window.onload = function () { console.log("load"); $("#whatevent").append("load<br/>"); } </script> </head> <body> <div id="whatevent"></div> </body> </html> 

none, but onload (but only the first time I load the page) events work on the ipad when I switch the tab.

+2
source share
1 answer

Someone asked this question two years ago with this question . Unfortunately, he was met with only a few answers, one of which seems to be the only way to achieve this effect. Until Apple can implement the full page visibility API in mobile safari, I remain using this custom object that I created that will use the API and return to the heart rate ticker if it is not available. However, as far as I can tell, there is no great way to test the inevitable switch of tablets.

Here is the main script of the object, demonstrating its only real method. It essentially just takes a handler function for the focus event, which fires when the browser returns the source tab. The backup is hacked at best and will work not only when you re-enter the page, but whenever the scripts stop longer than the timer threshold; which can be whenever the keyboard is visible, on a scroll, or if a running script prevents requestAnimationFrame from starting. Since scrolling is the most common behavior, I added a handler that resets the last saved time so that the focus event refrains from firing.

This is the main part of the script, which includes the β€œhacker" method, as described above:

  _that.onFocus = function(handler, params) { var hiddenProp = getHiddenProp(); console.log("Hidden prop: " + hiddenProp); if (hiddenProp) { var evtName = hiddenProp.replace(/[H|h]idden/, "") + "visibilitychange"; document.addEventListener(evtName, function(e) { if (isHidden()) { handler(e, params); } }, false); }else { var handlerObj = {"handler": handler}; if (params !== undefined) {handlerObj.params = params} _handlers.push(handlerObj); startLoop(); } }; 

The rest can be read in the violin. To see the backup, you will have to use a tablet (why else do you need this feature without it?).

Note that the .onFocus method can take an array of params for its second parameter, which is then passed to your event handler. This means that your event handler will always have an event object for it of the first parameter (or null if the API is not supported), and your array of parameters is the second parameter.

In addition, this code has not been tested for a couple of hours, so it may be subject to smoothing. I would appreciate any constructive criticism to make it worthy of production until Mobile Safari gets its butt.

+2
source

All Articles