[html5] Waiting for an object to be defined in javascript
Rik Sagar
org.whatwg at sagar.org
Thu May 22 12:13:29 PDT 2014
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.
Rik.
Rik Sagar, San Jose, CA 95124
Visit : http://sagar.org/
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/11674d98/attachment.htm>
More information about the Help
mailing list