简体   繁体   中英

How do I add a listener to JavaScript Object variable?

I have an object generated by this code , but I can only use it when its obj. ready property is == true. I can use only pure JavaScript or Prototypejs. It script is inserted as source (< script src=...).

Is there a way to detect when the object is ready to use? I tried to put observer on dom:loaded, but it didn't work.

I use

PagSeguroDirectPayment.setSessionId('someTextHere');
PagSeguroDirectPayment.getSenderHash(); 

The second line doesnt work because the object is not ready yet.

Any help is appreciated.

If you have no infrastructure of events there then the only option is to do polling:

function whenReady(obj, callback) {
  var iid = setInterval(function() {
    if(obj.ready) {
      callback();
      clearInterval(iid);
    }
  }, 20);
}

And to use it as:

PagSeguroDirectPayment.setSessionId('someTextHere');
whenReady(PagSeguroDirectPayment, function() {
  PagSeguroDirectPayment.getSenderHash(); 
}); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM