- // Set focus/blur events on the window
- $(window).on( 'blur.wp-heartbeat-focus', function() {
- setFrameFocusEvents();
- // We don't know why the 'blur' was fired. Either the user clicked in an iframe or outside the browser.
- // Running blurred() after some timeout lets us cancel it if the user clicked in an iframe.
- settings.winBlurTimer = window.setTimeout( function(){ blurred(); }, 500 );
- }).on( 'focus.wp-heartbeat-focus', function() {
- removeFrameFocusEvents();
- focused();
- }).on( 'unload.wp-heartbeat', function() {
+ // Switch the interval to 120 sec. by using the Page Visibility API.
+ // If the browser doesn't support it (Safari < 7, Android < 4.4, IE < 10), the interval
+ // will be increased to 120 sec. after 5 min. of mouse and keyboard inactivity.
+ if ( typeof document.hidden !== 'undefined' ) {
+ hidden = 'hidden';
+ visibilitychange = 'visibilitychange';
+ visibilityState = 'visibilityState';
+ } else if ( typeof document.msHidden !== 'undefined' ) { // IE10
+ hidden = 'msHidden';
+ visibilitychange = 'msvisibilitychange';
+ visibilityState = 'msVisibilityState';
+ } else if ( typeof document.webkitHidden !== 'undefined' ) { // Android
+ hidden = 'webkitHidden';
+ visibilitychange = 'webkitvisibilitychange';
+ visibilityState = 'webkitVisibilityState';
+ }
+
+ if ( hidden ) {
+ if ( document[hidden] ) {
+ settings.hasFocus = false;
+ }
+
+ $document.on( visibilitychange + '.wp-heartbeat', function() {
+ if ( document[visibilityState] === 'hidden' ) {
+ blurred();
+ window.clearInterval( settings.checkFocusTimer );
+ } else {
+ focused();
+ if ( document.hasFocus ) {
+ settings.checkFocusTimer = window.setInterval( checkFocus, 10000 );
+ }
+ }
+ });
+ }
+
+ // Use document.hasFocus() if available.
+ if ( document.hasFocus ) {
+ settings.checkFocusTimer = window.setInterval( checkFocus, 10000 );
+ }
+
+ $(window).on( 'unload.wp-heartbeat', function() {