#How to run a function that references itself?

2 messages · Page 1 of 1 (latest)

pine portal
#

In straight forward js this is very simple, but with qwik i am struglling to figure out ho to work around the scope issue which is being cause by trying to use "updateRandomImage" inside itself while being wrapped by $().

`export default component$(() => {

const promos = [];
const promoImages = useSignal([]);

const updateRandomImage = $(
function updateRandomImage() {
const indexToUpdate = Math.floor(Math.random() * promoImages.value.length);
promoImages.value[indexToUpdate] = promos[Math.floor(Math.random() * promos.length)];
setTimeout(updateRandomImage, Math.floor(Math.random() * (15000 - 5000 + 1)) + 5000);
}
);

useVisibleTask$(() => {
updateRandomImage()
});

return (
<>
{
promoImages.value.map((src: string) => (
<img src={src} />
))
}
</>
);
});`

low grove
#

Try like this

const updateRandomImage = $(() => {
const index //and other code
})