Having a nightmare with a function that ends before running all the code. I try to build a counter and return only after the code completes.
I imitated it this way (I know that this is not fantastic, but if someone could point me to the correct lines, I would be very grateful):
alert(timerCheck());
function timerCheck() {
var finished;
var myLoop = 5;
for (i = 0; i < myLoop; i++) {
window.setTimeout(checkFinished, 900);
alert(i);
}
function checkFinished() {
finished = true;
}
if (finished) {
return "done";
}
}
As I said, a very simplified example - if someone can point out an error, this will save me from the hassle!
source
share