How to wait for an element to be defined in javascript?
javascript
Solution
Have an interval running that keeps checking on the element:
var interval = setInterval(function() {
// get elem
if (typeof elem == 'undefined') return;
clearInterval(interval);
// the rest of the code
}, 10);
Problem
I need to do the equivalent of this in javascript: ``` while (typeof someObject == 'undefined') { sleep(10); // 10ms } ``` And I just can't quite figure out how to code this. I have this: ``` function sleep(ms, callback, arg) { setTimeout(function() { callback(arg); }, ms); } function waitForDef(elem) { if (typeof elem == 'undefined') { sleep(10, waitForDef, elem); } } ``` But it's not clear to me how to use this from my code.