I run these code on my firefox, and the console gets 3 2 1, which means statements run before the former ones end.
function test(){
setTimeout(function(){console.log(1)},1000); //statement 0
setTimeout(function(){console.log(2)},500); //statement 1
}
test();
console.log(3); //statement 2
Is it a feature of javascript, and do I have to double-check a variable after assigning it some value? Cuz’ it’s way too complicated.
var a;
while(a!==get('some')){
a=get('some');
if(a===get('some')){
whatShouldBeRun();afterGetSome();
break;
}
}
That is exactly what you have asked it to do. Firstly, you ask it to kick off two statements to run some point in the future. Then you write “3”. The first of those statements whose time it is to run then runs, and writes “2”. Finally, the third of these runs and writes “1”.
If you are using asynchronous processing – settimeout – then yes you need to handle this as with any other language. But then if you need it to run in order, don’t tell it to run asynchronously.
To get them to run one after the other:
[I might have some of this wrong]