[html5] Waiting for an object to be defined in javascript

Bill Barron bill.r.barron at gmail.com
Thu May 22 12:59:14 PDT 2014


Think of it this way. It is this mailing list's responsibility to send
email updates to its subscribers as they come in and not the subscribers
responsibility to check for updates every 10 seconds. This is not only less
resource intensive for the mailing list, it's also more efficient for the
subscribers.


On Thu, May 22, 2014 at 2:49 PM, Bill Barron <bill.r.barron at gmail.com>wrote:

> 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.
>
> 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:
>
> getSomeObject.then(function (someObject) {
>     // use someObject here
> });
>
> Thanks,
> Bill
>
>
> On Thu, May 22, 2014 at 2:41 PM, Larry Martell <larry.martell at gmail.com>wrote:
>
>> On Thu, May 22, 2014 at 3:13 PM, Rik Sagar <org.whatwg at sagar.org> wrote:
>> > Depends where the object comes from is and who's setting it!
>> >
>> > Couldn't you fire an event when the object is created, then do whatever
>> work
>> > you need to do in the event listener ?
>> >
>> > Alternatively, can "someObject" be an object inside "otherObject", for
>> > example, otherObject.someObject.
>> >
>> > If you have it that way, you can write a setter/getter on "otherObject"
>> for
>> > someObject.  When someone does "otherObject.someObject = new Object();"
>> your
>> > setter function gets called with the new value.
>> >
>> > Either is preferable to the busy loop approach if you can do it.
>>
>> The scenario is: After my page is loaded I need to call a js function
>> that will only exist some time after a table gets given a certain
>> class. When I try to call that function from an onload function it
>> fails most of the time because the function has not yet been defined.
>> If I call that function from a setTimeout that waits 1 second it works
>> 99% of the time. I'd like to just wait until the class gets put on the
>> table and the function exists and then call it.
>>
>> I think I may have this working. I did this:
>>
>> var interval = setInterval(function() {
>>     if (typeof elem == 'undefined') return;
>>     clearInterval(interval);
>>
>>     // the rest of the code
>> }, 10);
>>
>> So far this looks good. I need to run it for a day or 2 and make sure.
>>
>>
>> > On Thu, May 22, 2014 at 10:32 AM, Larry Martell <
>> larry.martell at gmail.com>
>> > wrote:
>> >>
>> >> 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.
>> _______________________________________________
>> Help mailing list
>> Help at lists.whatwg.org
>> http://lists.whatwg.org/listinfo.cgi/help-whatwg.org
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.whatwg.org/pipermail/help-whatwg.org/attachments/20140522/04d21a68/attachment-0001.htm>


More information about the Help mailing list