Stop/Pause WordPress Heartbeat using Javascript

I use the WordPress heartbeat to check a setting on my server from the front-end. If that setting has a specific value I want to stop bothering the server. There for I want to stop/pause the heartbeat. There are no other things depending on the heartbeat.

I found wp.heartbeat.stop() over here, but it doesn’t work.

jQuery(document).ready(function ($) {

    $(document).on('heartbeat-tick', function (e, data) {
        [...]
        if (true == x) {
            wp.heartbeat.stop();
        }
        [...]
    }

});

I get this error in the console:
TypeError: wp.heartbeat.stop is not a function
wp.heartbeat.stop();

Can’t find the stop() function in the heartbeat.js file.

This code doesn’t produce an error, but doesn’t work too.

jQuery(document).ready(function ($) {

    $(document).on('heartbeat-tick', function (e, data) {
        [...]
        if (true == x) {
            $(window).trigger('unload.wp-heartbeat');
        }
        [...]
    }

});

Do I trigger the unload.wp-heartbeat event the wrong way?

3 s
3

Drivingralle,

You are on the right trail here with your code.

I’ve done some work in the JavaScript console, and my conclusion from debugging heartbeat.min.js temporarily (source came from heartbeat.js) is that settings.suspend is properly triggered on the ‘unload.wp-heartbeat’ event. However, I believe it to be a bug that the focused() function sets settings.suspend back to false whenever the page is focused.

You could submit a bug indicating that the settings.suspend variable is inadvertently reverted in the focused() function after the unload.wp-heartbeat event is triggered and/or you could modify the heartbeat.(min).js code accordingly, perhaps including a new custom variable settings.permanentlySuspend that is then checked along with settings.suspend in the function scheduleNextTick().

Hope that helps,

Ryan

Leave a Comment