From b88e1c29038ac30e05157f0d3371569a22f55b99 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sat, 23 Nov 2019 18:11:34 +0100 Subject: [PATCH] update ticker immediately, if not prefilled by backend When a ticker is added by a Gutenberg block, it is initially empty. This can be detected by checking the "last" flag for 0 value. If found, the ticker is now updated immediately by the AJAX function and not waiting for the poll interval to trigger. --- scripts/liveticker.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/scripts/liveticker.js b/scripts/liveticker.js index f237e0e..c86c735 100644 --- a/scripts/liveticker.js +++ b/scripts/liveticker.js @@ -12,6 +12,8 @@ function scLiveticker() { * @return {void} */ scLiveticker.init = function() { + var updateNow = false; + // Opt out if AJAX pobject not present. if ( 'undefined' === typeof sclivetickerAjax ) { return; @@ -27,15 +29,21 @@ scLiveticker.init = function() { document.querySelectorAll( 'div.wp-block-scliveticker-ticker.sclt-ajax' ), function( elem ) { var list = elem.querySelector( 'ul' ); + var last = Number( elem.getAttribute( 'data-sclt-last' ) ); + if ( ! list ) { list = document.createElement( 'ul' ); elem.appendChild( list ); } + if ( 0 === last ) { + updateNow = true; + } + return { s: elem.getAttribute( 'data-sclt-ticker' ), l: elem.getAttribute( 'data-sclt-limit' ), - t: elem.getAttribute( 'data-sclt-last' ), + t: last, e: list, }; } @@ -46,15 +54,21 @@ scLiveticker.init = function() { document.querySelectorAll( 'div.wp-widget-scliveticker-ticker.sclt-ajax' ), function( elem ) { var list = elem.querySelector( 'ul' ); + var last = Number( elem.getAttribute( 'data-sclt-last' ) ); + if ( ! list ) { list = document.createElement( 'ul' ); elem.appendChild( list ); } + if ( 0 === last ) { + updateNow = true; + } + return { w: elem.getAttribute( 'data-sclt-ticker' ), l: elem.getAttribute( 'data-sclt-limit' ), - t: elem.getAttribute( 'data-sclt-last' ), + t: last, e: list, }; } @@ -62,7 +76,11 @@ scLiveticker.init = function() { // Trigger update, if necessary. if ( ( 0 < scLiveticker.ticker.length || scLiveticker.widgets.length ) && 0 < scLiveticker.pollInterval ) { - setTimeout( scLiveticker.update, scLiveticker.pollInterval ); + if ( updateNow ) { + scLiveticker.update(); + } else { + setTimeout( scLiveticker.update, scLiveticker.pollInterval ); + } } };