I need a function that waits until a variable comes into existence.
function wait(variable, callback) {
if (typeof variable !== "undefined")
callback();
else
setTimeout(function () {
wait(variable, callback);
}, 0)
}
Calling this function with the example code below causes an infinite loop.
var a;
wait(a, function(){console.log('success')});
setTimeout(function(){a=1}, 1000)
Why?