<div dir="ltr">I agree with Rick. You need to see what code is defining someObject or what code is setting the class on the table and when that code defines someObject it should trigger an event that the rest of your code listens for or provide some method of checking when it has loaded. If this is Modernizr and you're doing feature detection, then check out Modernizr.load(). (formerly YepNope). If you are just waiting for a script to load, you may be interested in AMD JavaScript modules. If your scripts are loaded and you are just waiting for some event to take place or some object to be defined, then modify the code that defines someObject or sets the table class or see if it has functionality built in already to notify the rest of your code that it is ready. It is that object's job to notify the rest of your code through events or promises that it has done it's part and defined someObject.<div>
<br></div><div>If the thing that defines someObject is your own code and not a 3rd party library or framework, consider adding a getSomeObject method that returns a promise. That way you could just do this:<div><br></div>
<div>getSomeObject.then(function (someObject) {<br> // use someObject here</div><div>});</div></div><div><br></div><div>Thanks,</div><div>Bill</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, May 22, 2014 at 2:41 PM, Larry Martell <span dir="ltr"><<a href="mailto:larry.martell@gmail.com" target="_blank">larry.martell@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="">On Thu, May 22, 2014 at 3:13 PM, Rik Sagar <<a href="mailto:org.whatwg@sagar.org">org.whatwg@sagar.org</a>> wrote:<br>
> Depends where the object comes from is and who's setting it!<br>
><br>
> Couldn't you fire an event when the object is created, then do whatever work<br>
> you need to do in the event listener ?<br>
><br>
> Alternatively, can "someObject" be an object inside "otherObject", for<br>
> example, otherObject.someObject.<br>
><br>
> If you have it that way, you can write a setter/getter on "otherObject" for<br>
> someObject. When someone does "otherObject.someObject = new Object();" your<br>
> setter function gets called with the new value.<br>
><br>
> Either is preferable to the busy loop approach if you can do it.<br>
<br>
</div>The scenario is: After my page is loaded I need to call a js function<br>
that will only exist some time after a table gets given a certain<br>
class. When I try to call that function from an onload function it<br>
fails most of the time because the function has not yet been defined.<br>
If I call that function from a setTimeout that waits 1 second it works<br>
99% of the time. I'd like to just wait until the class gets put on the<br>
table and the function exists and then call it.<br>
<br>
I think I may have this working. I did this:<br>
<br>
var interval = setInterval(function() {<br>
if (typeof elem == 'undefined') return;<br>
clearInterval(interval);<br>
<br>
// the rest of the code<br>
}, 10);<br>
<br>
So far this looks good. I need to run it for a day or 2 and make sure.<br>
<div class="HOEnZb"><div class="h5"><br>
<br>
> On Thu, May 22, 2014 at 10:32 AM, Larry Martell <<a href="mailto:larry.martell@gmail.com">larry.martell@gmail.com</a>><br>
> wrote:<br>
>><br>
>> I need to do the equivalent of this in javascript:<br>
>><br>
>> while (typeof someObject == 'undefined') {<br>
>> sleep(10); // 10ms<br>
>> }<br>
>><br>
>> And I just can't quite figure out how to code this.<br>
>><br>
>> I have this:<br>
>><br>
>><br>
>> function sleep(ms, callback, arg) {<br>
>> setTimeout(function() {<br>
>> callback(arg);<br>
>> }, ms);<br>
>> }<br>
>><br>
>> function waitForDef(elem) {<br>
>> if (typeof elem == 'undefined') {<br>
>> sleep(10, waitForDef, elem)<br>
>> }<br>
>> }<br>
>><br>
>> But it's not clear to me how to use this from my code.<br>
_______________________________________________<br>
Help mailing list<br>
<a href="mailto:Help@lists.whatwg.org">Help@lists.whatwg.org</a><br>
<a href="http://lists.whatwg.org/listinfo.cgi/help-whatwg.org" target="_blank">http://lists.whatwg.org/listinfo.cgi/help-whatwg.org</a><br>
</div></div></blockquote></div><br></div>