views:

1096

answers:

3

I have something like this:

function doSomething() {
   var obj = $('somediv');

   ...

   waitAndDoSomethingElse(obj);

   ...

   $('somediv').show();
   ...
}


function waitAndDoSomethingElse(obj) {
   obj.fadeIn();
   ....
}

I want doSomething() to pause... execute waitAndDoSomethingElse() and then continue... Any ideas?

Thank you

EDIT:

I'll try to explain better, sorry if my question was confused...

function showSomething() {
   var whatToShow = $('#div');

   doSomethingElse();

   whatToShow.fadeIn('slow');
}


function doSomethingElse() {
   $('#someDiv').appendTo($('#someOtherDiv'));
   $('#somethingElse').fadeIn('slow');
   ...
}

In this example whatToShow.fadeIn will fire without waiting for doSomethingElse to end...

+4  A: 

Use the animation callbacks. All the animations have them as the last arg. See here for the fadeIn docs.

 $('#someElement').fadeIn('slow', callbackFn);


   function callbackFn(){
       //executed after the fadeIn is complete
   }

If you are not using the animation helpers then you can use the same methodology and pass callback functions to other functions which can choose when to call the callback.

var callbackFn = function(){ //do something };

doSomething( callbackFn )

function doSomething( callback ){

    doOtherStuff();
    //call the callback
    callback && callback()

}

Another option is to use window.setTimeout to fire a function after x milliseconds.

redsquare
Yeah... i know about the callBack in the effects... I've put an effect just an example...what i need to do is the exact same thing but with generic functions...
noboruwatanabe
@noboruwatanabe: edit your question
Daniel Moura
Really, a duff question, and not doing yourself any favours with your un-friendly comments / responses
redsquare
upvote. this is the most correct and elegant way to do it.
Elzo Valugi
+1 This is how things should be done. (smile)
Christian Toma
A: 

Do this

function doSomething() {
    var obj = $('somediv');
    ...
    waitAndDoSomethingElse(obj);
}


function waitAndDoSomethingElse(obj) {
    obj.fadeIn();
    // if you want a pause here you can also add the next call in a setTimeout()
    $('somediv').show(); // this call is executed only after
}
Elzo Valugi
well I don't get the down votes... this solution is correct to his question. If somebody has better ones no problem, but a downvote means a wrong solution and should be explained. otherwise what is the point of helping?
Elzo Valugi
the problem here is... if i am executing something else in the doSomething function after the waitAndDoSomethingElse... that will be executed without waiting for the waitAndDoSomethingElse to end...back to the main issue
noboruwatanabe
He stated he doesn't want to use setTimeout and we also aren't really sure what he wants ... He downvoted everyone ...
Christian Toma
then pass callbacks around then you can choose when to call them...as per my answer
redsquare
@Christian Toma --> i didn't downvoted anyone... i cannot downvote, when i try to vote i get that i cannot do that because i don't have enough points...
noboruwatanabe
Ah but you did try eh!!
redsquare
$('somediv').show(); it will be executed last even if is in the waitAndDoSomethingElse function.
Elzo Valugi
Yes, it seems he did ... Good job. Please edit your question to a more clear one.
Christian Toma
but it will not wait for the fadeIn to complete. I think he wants that - but not sure:)
redsquare
+1 This works if you know how to use it. @noboruwatanabe
Christian Toma
if that .. is the callback... it wasn't even a question, just read the docs.
Elzo Valugi
A: 

You can only achieve what you want by using callbacks.

See redsquare's and Elzo Valugi's answers

Christian Toma