Reputation: 353
I have the next goal:
I want to have two functions - Start() and End(). The first function is runned at start of MyFunc(), the second is runned at end of MyFunc(). And End() shouldn't run while Start() isn't completed.
Now I wrote the next code:
function MyFunc(OnMyFuncReady) {
Start();
// do something
OnMyFuncReady();
}
function Start() {
// start specific actions
}
function End() {
// end specific actions
}
MyFunc(function() {
End();
});
But Start() running asynchronously in relation to End(). How I can solve the problem?
Update1: I check order of functions running and find that all is ok, but in Start() I use jQuery animate function, that run asynchronously...
Update2: Now I have the next problem: I want to run End() when Start() (where jQuery animate() is used) and MyFunc() are completed. Both. Difficult task for me.
Upvotes: 0
Views: 44
Reputation: 292
The ways as I see them are two:
first to wait until you do not receive any response from Start() and then to proceed. Once everything is finished, pass up an End function as a parameter. Unfortunately, you should separate somehow the bahviour of Start/End. The reason behind this is that you need to verify if start has finished. This kind of problem you should not have with End (release) function.
The second way is to create a variable with a value. Once you assigned Start, you will know what is going on. However, if your start fails somehow you'll probably should not want to proceed. Once Start is done, assign null value to that variable.After that you should check it for NULL and pass at the end the release function.
A good example of that is Jerry Cain's Stanford course, look here: https://www.youtube.com/watch?v=Yr1YnOVG-4g&feature=PlayList&p=9D558D49CA734A02&index=6
I hope I helped you. Cheers and think also for synchronous way if you have dependencies and use resources.
Upvotes: 1