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

@ -180,7 +180,8 @@ class WPLiveticker2 {
if ( 1 === self::$_options['enable_ajax'] ) {
$output .= ' wplt2-ticker-ajax" '
. 'data-wplt2-ticker="' . $atts['ticker'] . '" '
. 'data-wplt2-limit="' . $atts['limit'];
. 'data-wplt2-limit="' . $atts['limit'] . '" '
. 'data-wplt2-last="' . time();
}
$output .= '">';
@ -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);
}
});