public
Last active

WordPress 3.5 Compatible Heartbeat API Usage

  • Download Gist
enqueue-heartbeat.php
PHP
1 2 3 4 5
<?php
// enqueue the script,
// this fails gracefully if no 'heartbeat' script is registered
 
wp_enqueue_script('heartbeat');
heartbeat-monitor.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13
(function($) {
// watch for heartbeat event,
// no event will happen if the script isn't included
 
$(document).on('heartbeat-connection-lost', function() {
// do what you need to do here
$('body').addClass('connection-lost')
}).on('heartbeat-connection-restored', function() {
// do what you need to do here
$('body').removeClass('connection-lost')
});
 
})(jQuery);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.