I'm writting a small code to log a variable from a callback. So far my code looks like this.
function Run() {
GetNumber();
ShowNumber(); //--> (1)
}
Run();
var numero = null;
function GetNumber() {
numero = 12;
setTimeout(function() {
ShowNumber(); //--> (2)
}, 2000);
}
function ShowNumber() {
console.log(numero);
}
Notice the (1) and (2). When (1) is called, the log is "12" as expected. But when (2) is called after 2 seconds, it doesn't work and throws this in the console:
null
I've tried adding bind(this) to the annonymous callback but still, the same error. Why is this happening?.