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
}
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
}
}