First of all check how many entries you have.
Then, use Math.random() * total to pick up a randomIndex.
Then run a query with skip(randomIndex).limit(4)
if the randomIndex is one of the last 3, get also the items from the beginning.
You wrote: let max = 4 ;
However max should be the total number of entries.
Check the total number first and then pick up the random number.
If you want to save time, run the function on a backend jsw file.
Ive copied those exactly, frontend into the page tab and backend into the site tab however I am getting errors on lines 4 and 15 with WIXDATA underlined in Red.
The backend code shouldn’t be on the site tab.
It should be in a jsw file on the backend.
You need to add to the backend code: import wixData from ‘wix-data’;
P.S.
Instead of using backend code, you can put everything on the page code panel (and remove the import from backend line) . However it might be slightly slower (you can try it).
Hi, Just wanted to say thank you for this thread. After days of trying to achieve the same effect I have it working.
I do however get the following console error;
Wix code SDK Warning: The text parameter of “text40” that is passed to the text method cannot be set to null or undefined.
text40 is the field in the dynamic repeater on the dynamic page and is only referred to in the code on this line; const repeatedText = $w ( “#text40” );
repeatedText . text = itemData . text ;
To be clear the effect of randomising the results in the dynamic repeater on the dynamic page is working, i just have this console error.