I have a script that will be executed before the variable it must read will be loaded..
this is my script that will be executed first
funcThatWaits(varToWait).ready(function(){
//callback time!!
alert("varToBeWait is Ready!!");
});
and this is the script that will be next to be loaded
var varToWait=null;
All I want is to create a function that will wait to a variable to exist and do a callback automatically when it detects the variable already existed.(This means while the variable does not exist it will wait)
Is this possible? my first script is quite copied on the $(document).ready() function of jquery that waits for the DOM to be fully loaded… is this possible for JS variables?
If your variable comes from another function (and maybe from another scope), then you can pass a callback and provide it the variable when the second function executes the callback. you don’t need to wait when it will exist, but you will wait until the second script provides it for you.
another way to do it is to have a loader script defined beforehand to aggregate callbacks and call them once their variables are set: