0

I know this is a simple question, but I don't really know how to do it. I'm executing two functions in my code, 'luxboxEngine' and 'fitToScreen', the latter of which requires the completion of the former. How do I tell 'fitToScreen' to only execute after 'luxboxEngine' has completed? Right now I have this, which gets the desired results:

luxboxEngine(self);
setTimeout(fitToScreen, 1000);

...But I know that's not a good solution. I've read about callback functions, but I'm afraid I don't really get what they are / how to incorporate them. Thanks for reading.

EDIT: So as Sudharsan Sri and Brian McGinity answered below, a callback is the solution. Here's the code I used to get what I need in my program:

    luxboxEngine(self, function () {
       fitToScreen(); 
    });

That fires the fitToScreen function after the completion of luxboxEngine.

7
  • What is "luxboxEngine" and how does it work? Is it synchronous? Asynchronous? If it's asynchronous and doesn't provide a callback or Promise mechanism (it should, see the documentation), then you're up a creek without a paddle and have to fall back to flailing wildly (e.g. setTimeout or other hack) in the canoe. Commented Feb 11, 2014 at 3:52
  • Self means 'this' in Javascript Commented Feb 11, 2014 at 3:55
  • luxboxEngine animates a whole bunch of stuff, and self is a variable passed as a parameter containing a picture to be animated. Commented Feb 11, 2014 at 3:55
  • @Ber Then it has "complete" events, no? Commented Feb 11, 2014 at 3:55
  • 1
    @Ber, I just updated my answer... the function being passed as an argument waits to be called... passing it as an argument is like defining a function, it only runs when called. Commented Feb 11, 2014 at 4:16

3 Answers 3

1

Use a callback:

luxboxEngine(self , function() { fitToScreen(); }); 
Sign up to request clarification or add additional context in comments.

1 Comment

Welcome It's my pleasure
1

like a callback? Pass the function as a argument then call it:

 fitToScreen( function(){luxBoxEginie();}  );

 function fitToScreen( cb ) {
  //// do somethings
  cb()
 }

or

luxboxEngine(self, function (){ fitToScreen(); });

function luxboxEngine( s, cb)} {
 // do somethings
 cb();  //execute the callback function
}

fitToScreen() is passed as an argument into luxboxEngine(). fitToScreen() waits to to be executed, and runs only when you do: cb()

1 Comment

Yep a callback is the solution.
0
luxboxEngine(self); //Executes, returns undefined, nothing happens with return value
fitToScreen(); //The first one is done by the time we get here

Comments

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.