I am using a function supplied by a third party library. This function takes a callback function as a parameter, but I would like to wait for this callback to be called before continuing. Is there a standard / accepted way to do this?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
I am not sure if this is a possible solution for you but you can achieve the desired result by breaking your code into 2 functions. Suppose this is what you intend to do:
Basically this is your original function:
You can modify the code flow with something like:
This will eliminate the wait loop. And as far as I know, busy waiting doesn't work in IE (because ? God only knows)..
I don't know if Sharad's solution would work in all cases (for instance if you function calls are so far chained that you have to pass data as variables instead of parameters). Same problem with global variables. JavaScript just doesn't have a "wait" ability.
Although I had a similar problem and with jQuery, I ended up with a MacGyver type solution that gives you tons of control on when javascript functions execute. I just posted it here as an answer to my own question (but I it's an answer that's not checked - look for my username Emile): How to get a variable returned across multiple functions - Javascript/jQuery
Here's another method of loading jQuery asynchronously, which doesn't depend on another script.