Tuesday, May 21, 2024
 Popular · Latest · Hot · Upcoming
167
rated 0 times [  170] [ 3]  / answers: 1 / hits: 58108  / 13 Years ago, fri, october 7, 2011, 12:00:00

I have two javascript functions



function one () {
do something long... like writing jpgfile on disk
}

function two () {
do something fast... like show the file
}


I call it (in jQuery) like this



 one ();
two ();


Because function two needs the link file from function one, i need to be sure the execution is completed... so getting the function two in the callback of function one should be the trick.. but how to do that ?



note : I did put an alert ('aaa') between those two functions to let function one complete, and it worked fine... when the alert is commented (removed) nothing works anymore !


More From » jquery

 Answers
13

You only need to use a callback if you are doing something asynchronous, otherwise it doesn't matter how long something takes, the next function won't run until the first has finished.



A callback is just passing a function as an argument, and then calling it when done.



function one (callback) {
do something long... like writing jpgfile on disk
callback();
}

function two () {
do something fast... like show the file
}

one(two);


Obviously, if you are doing something asynchronous, then you need something that will tell you when it is finished (such as an event firing).


[#89736] Thursday, October 6, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tai

Total Points: 466
Total Questions: 87
Total Answers: 116

Location: Saint Helena
Member since Tue, Nov 3, 2020
4 Years ago
;