0

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.

2
  • 1
    Please post the code here. Reduce it to the most relevant part only. Commented Apr 12, 2014 at 23:03
  • Hi Felix. The code in the library is not relevant, but I'll add other parts here. Commented Apr 12, 2014 at 23:14

1 Answer 1

2

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(); 
}); 
Sign up to request clarification or add additional context in comments.

2 Comments

I thought about interval, but I'm afraid that there's a less agressive option. =) But it works. Thank you very much.
@RicardoMartins Another approach would be to use Object.observe(callback) so without polling but it a) didn't land into browsers widely and b) not too much less "aggressive" than polling. But I suspect that code you citing has mechanism of events allowing you to subscribe on "ready" event.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.