My SetTimeout Loop function doesnt appear to work in Backend code.

I have a simple delay loop that logs to console every 1s for 9 loops, it works perfectly on page code but when i try and call it from backend code i only get the first iteration and then nothing, any suggestions on what i have done wrong.

{
var i = 1; // set your counter to 1
function myLoop () { // create a loop function
setTimeout( function () { // call a 1s setTimeout when the loop is called
console.log(‘hello’); // Log To console
i++; // increment the counter
if (i < 10) { // if the counter < 10, call the loop function
myLoop(); // … again which will trigger another
} // … setTimeout()
}, 1000)
}
myLoop(); // st
}

Hi,
please send us either one of the following:

  • Full backend code for the timeout function and front end code of how you call it.

  • Or the link to the editor, site page and name of the backend file.
    When we have this information we’ll be able to check further.

The code above is all the code.

Its called Via button press with either:
Front end code:
export function button6_click(event) {
{
var i = 1; // set your counter to 1
function myLoop() { // create a loop function
setTimeout( function () { // call a 1s setTimeout when the loop is called
console.log(‘hello’); // Log To console
i++; // increment the counter
if (i < 10) { // if the counter < 10, call the loop function
myLoop(); // … again which will trigger another
} // … setTimeout()
}, 1000)
}
myLoop(); // st
}
}

or Backend Code called via button press

export function button6_click(event) {
delay()
}

function delay() {
{
var i = 1; // set your counter to 1
function myLoop() { // create a loop function
setTimeout( function () { // call a 1s setTimeout when the loop is called
console.log(‘hello’); // Log To console
i++; // increment the counter
if (i < 10) { // if the counter < 10, call the loop function
myLoop(); // … again which will trigger another
} // … setTimeout()
}, 1000)
}
myLoop(); // st
}
}