Can function be executed after some variable got value (or changed) in JavaScript?
In my example, the variable
is hoisted at first which is declared to be a global, then will assign some value to it in getData()
function.
I have tried using .delay()
method, but i don't know how much time the ajax needs.
briefly describe:
1) Declaring variable
2) calling getData()
function, then assign the data to variable
3) calling funcA()
function
4) calling funcB()
function and pass function doSomething()
as a callback in funcA()
5) calling the callback function in funcB()
6) log the data in doSomething()
<script>
var variable;
variable = getData();
funcA();
function funcA() {
funcB(doSomething);
function doSomething(data) {
console.log(data);
}
}
function funcB(callback) {
if (variable !== undefined) {//it is obviously undefined.
callback(variable);
}
}
</script>
//another js file
<script>
function getData() {
//get data by using ajax
//i don't know how much time it needs.
return data;
}
</script>