221 // Use document.hasFocus() if available. |
221 // Use document.hasFocus() if available. |
222 if ( document.hasFocus ) { |
222 if ( document.hasFocus ) { |
223 settings.checkFocusTimer = window.setInterval( checkFocus, 10000 ); |
223 settings.checkFocusTimer = window.setInterval( checkFocus, 10000 ); |
224 } |
224 } |
225 |
225 |
226 $(window).on( 'unload.wp-heartbeat', function() { |
226 $(window).on( 'pagehide.wp-heartbeat', function() { |
227 // Don't connect anymore. |
227 // Don't connect anymore. |
228 settings.suspend = true; |
228 suspend(); |
229 |
229 |
230 // Abort the last request if not completed. |
230 // Abort the last request if not completed. |
231 if ( settings.xhr && settings.xhr.readyState !== 4 ) { |
231 if ( settings.xhr && settings.xhr.readyState !== 4 ) { |
232 settings.xhr.abort(); |
232 settings.xhr.abort(); |
233 } |
233 } |
234 }); |
234 }); |
|
235 |
|
236 $(window).on( |
|
237 'pageshow.wp-heartbeat', |
|
238 /** |
|
239 * Handles pageshow event, specifically when page navigation is restored from back/forward cache. |
|
240 * |
|
241 * @param {jQuery.Event} event |
|
242 * @param {PageTransitionEvent} event.originalEvent |
|
243 */ |
|
244 function ( event ) { |
|
245 if ( event.originalEvent.persisted ) { |
|
246 /* |
|
247 * When page navigation is stored via bfcache (Back/Forward Cache), consider this the same as |
|
248 * if the user had just switched to the tab since the behavior is similar. |
|
249 */ |
|
250 focused(); |
|
251 } |
|
252 } |
|
253 ); |
235 |
254 |
236 // Check for user activity every 30 seconds. |
255 // Check for user activity every 30 seconds. |
237 window.setInterval( checkUserActivity, 30000 ); |
256 window.setInterval( checkUserActivity, 30000 ); |
238 |
257 |
239 // Start one tick after DOM ready. |
258 // Start one tick after DOM ready. |
539 */ |
558 */ |
540 function focused() { |
559 function focused() { |
541 settings.userActivity = time(); |
560 settings.userActivity = time(); |
542 |
561 |
543 // Resume if suspended. |
562 // Resume if suspended. |
544 settings.suspend = false; |
563 resume(); |
545 |
564 |
546 if ( ! settings.hasFocus ) { |
565 if ( ! settings.hasFocus ) { |
547 settings.hasFocus = true; |
566 settings.hasFocus = true; |
548 scheduleNextTick(); |
567 scheduleNextTick(); |
549 } |
568 } |
550 } |
569 } |
551 |
570 |
552 /** |
571 /** |
|
572 * Suspends connecting. |
|
573 */ |
|
574 function suspend() { |
|
575 settings.suspend = true; |
|
576 } |
|
577 |
|
578 /** |
|
579 * Resumes connecting. |
|
580 */ |
|
581 function resume() { |
|
582 settings.suspend = false; |
|
583 } |
|
584 |
|
585 /** |
553 * Runs when the user becomes active after a period of inactivity. |
586 * Runs when the user becomes active after a period of inactivity. |
554 * |
587 * |
555 * @since 3.6.0 |
588 * @since 3.6.0 |
556 * @access private |
589 * @access private |
557 * |
590 * |
591 } |
624 } |
592 |
625 |
593 // Suspend after 10 minutes of inactivity when suspending is enabled. |
626 // Suspend after 10 minutes of inactivity when suspending is enabled. |
594 // Always suspend after 60 minutes of inactivity. This will release the post lock, etc. |
627 // Always suspend after 60 minutes of inactivity. This will release the post lock, etc. |
595 if ( ( settings.suspendEnabled && lastActive > 600000 ) || lastActive > 3600000 ) { |
628 if ( ( settings.suspendEnabled && lastActive > 600000 ) || lastActive > 3600000 ) { |
596 settings.suspend = true; |
629 suspend(); |
597 } |
630 } |
598 |
631 |
599 if ( ! settings.userActivityEvents ) { |
632 if ( ! settings.userActivityEvents ) { |
600 $document.on( 'mouseover.wp-heartbeat-active keyup.wp-heartbeat-active touchend.wp-heartbeat-active', function() { |
633 $document.on( 'mouseover.wp-heartbeat-active keyup.wp-heartbeat-active touchend.wp-heartbeat-active', function() { |
601 userIsActive(); |
634 userIsActive(); |