6

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
    Sincerely good luck with this. I looked at it before and it's an abomination of an API, that got turned upside down along the way and has plenty of now incorrect info about. – Rarst May 15 '15 at 17:28
  • 2
    The only thing I got working to ameliorate this was setting the minimal interval to the maximum 10 minutes: add_filter( 'heartbeat_settings', function ( $settings ) { $settings['minimalInterval'] = 600; return $settings; } ); – bonger May 15 '15 at 21:26
  • Would have suggested you try jQuery(window).trigger( 'unload.wp-heartbeat' ) but strangely that does nothing. Perhaps you can workout why? – Jan Beck May 16 '15 at 22:26
  • I cannot find any documentation of the heartbeat api. That's sad. – gdaniel May 20 '15 at 19:47
  • You can help write the documentation here codex.wordpress.org/Function_Reference/wp_heartbeat_settings – Tim Hallman May 20 '15 at 22:40
1

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

|improve this answer|||||
  • I saw this in the heartbeat.js file at line 631: case 'long-polling': Does that get us further? – Drivingralle Jun 4 '15 at 16:18
  • @Drivingralle Not sure it does. You want to stop polling altogether. I think the only options are still to a) submit a bug or b) hack the heartbeat.js code so that the focused() function does not un-suspend anything. The lightest footprint if you go with option b is probably to create the settings.permanentlySuspend option that also gets set whenever you trigger the unload.wp-heartbeat event. – ryanm Jun 12 '15 at 23:07
0

Hey i was also looking for same solution.

So i have this chat application and i am using polling kind of activity using heatbeat now if when chat closes i don’t want any traffic on my site which is useless so i want to disable heartbeat but unfortunately wordpress do not allow to disable heartbeat, I see it was security reason, There was this stop function before, but they removed.

We have settings option in wp.heartbeat where suspend variable is stored but it is not accessible. these are the methods that are accessible using wp.heartbeat.

connectNow: r()
dequeue: w(a)
disableSuspend: s()
enqueue: u(a,b,c)
getQueuedItem: x(a)
hasConnectionError: q()
hasFocus: p()
interval: t(a,b)
isQueued: v(a)

so all i can do is slow down the ajax calls using wp.heartbeat.interval( 120 ); where 120 is max time we can set which is about 2 min.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.