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.
This commit is contained in:
parent
fc4783d07a
commit
b88e1c2903
@ -12,6 +12,8 @@ function scLiveticker() {
|
|||||||
* @return {void}
|
* @return {void}
|
||||||
*/
|
*/
|
||||||
scLiveticker.init = function() {
|
scLiveticker.init = function() {
|
||||||
|
var updateNow = false;
|
||||||
|
|
||||||
// Opt out if AJAX pobject not present.
|
// Opt out if AJAX pobject not present.
|
||||||
if ( 'undefined' === typeof sclivetickerAjax ) {
|
if ( 'undefined' === typeof sclivetickerAjax ) {
|
||||||
return;
|
return;
|
||||||
@ -27,15 +29,21 @@ scLiveticker.init = function() {
|
|||||||
document.querySelectorAll( 'div.wp-block-scliveticker-ticker.sclt-ajax' ),
|
document.querySelectorAll( 'div.wp-block-scliveticker-ticker.sclt-ajax' ),
|
||||||
function( elem ) {
|
function( elem ) {
|
||||||
var list = elem.querySelector( 'ul' );
|
var list = elem.querySelector( 'ul' );
|
||||||
|
var last = Number( elem.getAttribute( 'data-sclt-last' ) );
|
||||||
|
|
||||||
if ( ! list ) {
|
if ( ! list ) {
|
||||||
list = document.createElement( 'ul' );
|
list = document.createElement( 'ul' );
|
||||||
elem.appendChild( list );
|
elem.appendChild( list );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( 0 === last ) {
|
||||||
|
updateNow = true;
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
s: elem.getAttribute( 'data-sclt-ticker' ),
|
s: elem.getAttribute( 'data-sclt-ticker' ),
|
||||||
l: elem.getAttribute( 'data-sclt-limit' ),
|
l: elem.getAttribute( 'data-sclt-limit' ),
|
||||||
t: elem.getAttribute( 'data-sclt-last' ),
|
t: last,
|
||||||
e: list,
|
e: list,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -46,15 +54,21 @@ scLiveticker.init = function() {
|
|||||||
document.querySelectorAll( 'div.wp-widget-scliveticker-ticker.sclt-ajax' ),
|
document.querySelectorAll( 'div.wp-widget-scliveticker-ticker.sclt-ajax' ),
|
||||||
function( elem ) {
|
function( elem ) {
|
||||||
var list = elem.querySelector( 'ul' );
|
var list = elem.querySelector( 'ul' );
|
||||||
|
var last = Number( elem.getAttribute( 'data-sclt-last' ) );
|
||||||
|
|
||||||
if ( ! list ) {
|
if ( ! list ) {
|
||||||
list = document.createElement( 'ul' );
|
list = document.createElement( 'ul' );
|
||||||
elem.appendChild( list );
|
elem.appendChild( list );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( 0 === last ) {
|
||||||
|
updateNow = true;
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
w: elem.getAttribute( 'data-sclt-ticker' ),
|
w: elem.getAttribute( 'data-sclt-ticker' ),
|
||||||
l: elem.getAttribute( 'data-sclt-limit' ),
|
l: elem.getAttribute( 'data-sclt-limit' ),
|
||||||
t: elem.getAttribute( 'data-sclt-last' ),
|
t: last,
|
||||||
e: list,
|
e: list,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -62,8 +76,12 @@ scLiveticker.init = function() {
|
|||||||
|
|
||||||
// Trigger update, if necessary.
|
// Trigger update, if necessary.
|
||||||
if ( ( 0 < scLiveticker.ticker.length || scLiveticker.widgets.length ) && 0 < scLiveticker.pollInterval ) {
|
if ( ( 0 < scLiveticker.ticker.length || scLiveticker.widgets.length ) && 0 < scLiveticker.pollInterval ) {
|
||||||
|
if ( updateNow ) {
|
||||||
|
scLiveticker.update();
|
||||||
|
} else {
|
||||||
setTimeout( scLiveticker.update, scLiveticker.pollInterval );
|
setTimeout( scLiveticker.update, scLiveticker.pollInterval );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user