Heartbeat API: How to access data already enqueued?

I'm having problems when trying to access data that I've queued using the Heartbeat API.

I'm queuing data using wp.heartbeat.enqueue(). For example:

var data = { 
    my_key: 'value'
};
wp.heartbeat.enqueue( 'my_handle', data, false );

Then later on in my script, I'd like to access the data I've queued. For example:

$( document ).on( 'click', '#button', function() {
    // Here, I want to access the data I've enqueued.
});

I've tried doing the following but that doesn't work:

$( document ).on( 'click', '#button', function() {
    var my_data = wp.heartbeat.getQueuedItem( 'my_handle' );
    alert( my_data.data.my_key );
});

Hoping someone can suggest where I'm going wrong?

Topic heartbeat-api jquery Wordpress javascript

Category Web


I found the solution to my problem was to change my_data.data.my_key to my_data.my_key. For example:

$( document ).on( 'click', '#button', function() {
    var my_data = wp.heartbeat.getQueuedItem( 'my_handle' );
    alert( my_data.my_key );
});

The queued data can be easily accessed now.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.