Cron expression to trigger a repeater filled with random images

Hi wix community !
my goal is to make a repeater with images which change every day randomly with a cron exression.
I succeed to change the images template by putting the code in public file, but since I can’t call a job scheduler in public and only backend, I’m having issue

Here is my Home code page :

-----------------------HOME---------------------------------------------------
import {makeRepeater} from ‘public/rubriqueConfig.js’;
import {onceADay} from ‘backend/rubriqueBackend.jsw’;

$w.onReady( async function() {
onceADay()
} );

----------------------------rubriqueConfig.js---------------------------------------------
import wixData from ‘wix-data’;

export function makeRepeater(listNumber) {
let dataset = wixData.query(“rubrique”);
dataset.find()
.then( (results) => {
let datasetLength = results.length;
$w(“#repeater”).forEachItem( ($item, itemData, index) => {
$item(“#imageRepeater”).src = results.items[listNumber[index]].image;
})
})
.catch( (error) => {
let errorMsg = error.message;
let code = error.code;
console.log(errorMsg, code);
});
}
----------------------------rubriqueBackend.jsw---------------------------------------------
import {makeRepeater} from ‘public/rubriqueConfig.js’;

let listNumber = ;
const numberOfImage = 6;

// this works fine
function getNumber(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min +1)) + min;
}
// this works fine
function getListNumber() {
listNumber.push(getNumber(1,23));
for (var i=0; i<5 ; i++) {
let number = getNumber(1,23);
while (listNumber.includes(number)) {
number = getNumber(1,23);
}
listNumber.push(number);
}
}

export function onceADay() {
getListNumber(); // we fill the list with random number
makeRepeater(listNumber);
}
----------------------------------------------jobs.config--------------------------------
// /backend/jobs.config
{
“jobs”: [ // Define up to 20 different jobs
// Choose one of the options below to define the job execution interval:
{
// Option 1 - define execution interval using a cron expression
“functionLocation”: “/rubriqueBackend.jsw”, // Relatively to Backend folder, started by slash
“functionName”: “onceADay”,
“description”: “change rubrique images disposition”, // Optional
“executionConfig”: {
“cronExpression”: “* * * * *” // Set intervals every minutes
// Read about Cron expressions: [cron - Wikipedia]
}
},
{
// Option 2 - define execution interval by setting time, day of week and day of month
“functionLocation”: “/rubriqueBackend.jsw”, // Relatively to Backend folder, started by slash
“functionName”: “onceADay”,
“description”: “describe your job”, // Optional
“executionConfig”: {
“time”: “22:00” // “hh:mm” 24h format, UTC timezone (e.g. 13:00)
// Optional - uncomment section below for more complex intervals
//“dayOfWeek”: “Monday”, // Day of week
//“dateInMonth”: 1 // Number between 1-31
}
}
]
}

I get the $w is not defined null, I look and find from a non official source that $ can be used only in a onReady function ? That cron expression can be called only from backend, I feel there is an issue between my backen and frontend If you have any idea, i’ll be glad !
Thanks

Hey there,

The way to do it is to store the repeater items in a DB, and always retrieve them from there when you want to display these items.

In your job scheduler, create a function that creates random items, and replace the items in the DB with the new ones, then sends a real-time notification to a public channel (make sure to add an event listener on the repeater page).

On the page that the repeater resides on, add an event listener to the public channel, and whenever a notification is received, re-fetch the latest items from the DB and update the repeater.

It should take you about an hour to implement this.

Hope this helps~!
Ahmad