@DownPW As JavaScript (unless it’s server side) has no access to the file system, we can’t use a native NodeJS function like the below to list the files
const testFolder = '/home/nodebb/nodebb/public/customlogo/afternoon';
const fs = require('fs');
fs.readdir(testFolder, (err, files) => {
files.forEach(file => {
console.log(file);
});
});
It’s pretty simple to do this with PHP, and then pass back the variables to JS for client side processing. However, if you don’t have PHP installed, then of course this won’t work. However, it is possible to form an array in jQuery and use that with a timer to change the image. This is actual working code designed in your test environment
NB - this is a proof of concept, and is not the completed script. The idea is to test the functionality, and then if it works as desired, we increase the scope, and implement across the original code we already have
// ------------------------------------------
// Rotate footer images at 60 second interval
// ------------------------------------------
// Provide a list of the files we need, less the .png extension as we add this automatically
var files = ['01', '02', '03'];
// Set the path where the images are stored
var thepath = "/assets/customlogo/cycle/evening/";
// Timer starting point
var offset = 0;
// Loop through each array item (files), and after 60 seconds, replace the current image with the next index in the array.
// We fade out the image first, then fade in the new one (0.4 seconds)
$(files).each(function(logo, index) {
setTimeout(function() {
$("#thislogo").fadeOut('slow', function() {
$('#thislogo').on('load', function() {
$('#thislogo').fadeIn(400);
}).attr("src", thepath + index + '.png');
});
// Place a log in the console so we can see that the script is active
console.log("Switching footer image to " + thepath + index + ".png");
}, 60000 + offset);
// Add another 60 seconds and restart the loop on the next array index
offset += 60000;
// Once the file array is exhausted, exit
});
This initially loads the selected image according to the previous code I provided, then waits 60 seconds, and changes to the next in the cycle, so 01, 02, 03 etc until it reaches the end where it will exit.
Important
- The script will start again and add the default image selected for that time period if the page is reloaded, or the home logo / text is clicked, as this is the equivalent of reloading the site.
- The script does not care what you are doing at the time of change, as it uses it’s own timer outside of any callback functions in NodeBB - it will execute regardless
- The exit is graceful, and designed not to chew the CPU or memory of the visiting host. The recommendation would be to take the time period, divide it by the number of images you have, and then alter the 60 second value (denoted as
60000
) to the value you land up with.
- The
interval
is recorded in milliseconds
(see below for calculation)
So, based on this from the previous script
if (thehours >= 0 && thehours < 6) {
themessage = night;
theicon = "fa-solid fa-moon";
thelogo = "/assets/customlogo/night/pw_night.png";
}
Note that an additional }
has been added here for cosmetic purposes, and does not appear in the original script
6 hours (or 360,000ms = 60 minutes x 6 = 360) with 4 images would equate to 60000 x 60 = 3600000 / 4
meaning your interval period would be 900000
milliseconds or 15 minutes.
My advice would be to play with this first to get yourself familiar with the concept, and then we can factor into the previously provided code to make the one function