首先,你需要给他发出滴答声的日期:
// Hook into the heartbeat-send and give him the milliseconds timestamp since epoch
$(document).on(\'heartbeat-send\', function(e, data) {
data[\'now\'] = new Date().getTime();
});
我们已经准备好了场地,现在让我们倾听心跳的时刻:
$(document).on(\'heartbeat-tick\', function(e, data) {
alert(data);
});
现在是php部分:
$then = 0;
function my_heartbeat($response, $data)
{
global $then;
$then = date("Y-m-d H:i:s", $data[\'then\']);
add_filter(\'posts_where\', \'filter_where_date\');
$query = new WP_Query(array(\'post_type\' => \'post\'));
$response[\'number\'] = $query->found_posts;
remove_filter(\'posts_where\', \'filter_where_date\');
return $response;
}
add_filter(\'heartbeat_received\', \'my_heartbeat\', 10, 3);
function filter_where($where = \'\')
{
global $then;
$where .= " AND post_date >= {$then} AND post_date < NOW()";
return $where;
}
希望这有帮助。