0

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...

1
  • You should change your question to be more clear. We are having problems understanding exactly what you want. Maybe another example, or a test case. Commented Jul 30, 2009 at 15:11

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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...
Really, a duff question, and not doing yourself any favours with your un-friendly comments / responses
0

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
}

9 Comments

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?
then pass callbacks around then you can choose when to call them...as per my answer
$('somediv').show(); it will be executed last even if is in the waitAndDoSomethingElse function.
but it will not wait for the fadeIn to complete. I think he wants that - but not sure:)
if that .. is the callback... it wasn't even a question, just read the docs.
|

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.