Answer the question
In order to leave comments, you need to log in
How to change the value of a variable after recursive setTimeout calls have finished?
variable = false;
function aaa() {
bbb();
variable = true;
}
function bbb() {
if(//expression exit) return;
SetTimeout(bbb, 2000);
}
aaa(); // call main function
Answer the question
In order to leave comments, you need to log in
You don't seem to understand asynchronous work in JS. The SetTimeout function does not wait for the specified time (you have 2 seconds). She sets the timer and returns immediately. The function specified in SetTimeout after the specified time will be queued for JS calls and launched when the queue reaches it.
Thus, the function bbb called from aaa will complete almost immediately. You can write like this:
function bbb() {
if(...) {
variable = true;
return;
}
SetTimeout(bbb, 2000);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question