Only send new Ticks on AJAX update

THis reduces the overhead of re-sending huge amounts of HTML code to the frontend without any new data to display.
This commit is contained in:
Stefan Kalscheuer 2017-12-09 11:48:23 +01:00
parent 5158b8fdb7
commit de347e1f81
2 changed files with 16 additions and 7 deletions

View File

@ -179,8 +179,9 @@ class WPLiveticker2 {
$output = '<ul class="wplt2-ticker';
if ( 1 === self::$_options['enable_ajax'] ) {
$output .= ' wplt2-ticker-ajax" '
. 'data-wplt2-ticker="' . $atts['ticker'] . '" '
. 'data-wplt2-limit="' . $atts['limit'];
. 'data-wplt2-ticker="' . $atts['ticker'] . '" '
. 'data-wplt2-limit="' . $atts['limit'] . '" '
. 'data-wplt2-last="' . time();
}
$output .= '">';
@ -266,8 +267,8 @@ class WPLiveticker2 {
$res = array();
foreach ( wp_unslash( $_POST['update'] ) as $update_req ) {
if ( isset( $update_req['s'] ) ) {
$slug = $update_req['s'];
$limit = ( isset( $update_req['l'] ) ) ? intval( $update_req['l'] ) : - 1;
$slug = $update_req['s'];
$limit = ( isset( $update_req['l'] ) ) ? intval( $update_req['l'] ) : - 1;
$last_poll = ( isset( $update_req['t'] ) ) ? intval( $update_req['t'] ) : 0;
// Query new ticks from DB.
@ -281,6 +282,9 @@ class WPLiveticker2 {
'terms' => $slug,
),
),
'date_query' => array(
'after' => date( 'c', $last_poll ),
)
);
$query = new WP_Query( $query_args );

View File

@ -42,9 +42,14 @@ WPLT2.update = function () {
jQuery.each(update, function (i, u) {
jQuery.each(WPLT2.ticker, function (j, t) {
if (t.s === u.s) {
// Set HTML content.
jQuery(t.e).html(u.h);
// Set last poll timestamp.
// Prepend HTML of new ticks.
jQuery(t.e).prepend(u.h);
// Remove tail, if limit is set.
const l = jQuery(t.e).data('wplt2Limit');
if (l > 0) {
jQuery(t.e).find('li').slice(l).remove();
}
// Update last poll timestamp.
jQuery(t.e).data('wplt2Last', u.t);
}
});