If I need call this functions one after other,
$('#art1').animate({'width':'1000px'},1000);
$('#art2').animate({'width':'1000px'},1000);
$('#art3').animate({'width':'1000px'},1000);
I know in jQuery I could do something like:
$('#art1').animate({'width':'1000px'},1000,'linear',function(){
$('#art2').animate({'width':'1000px'},1000,'linear',function(){
$('#art3').animate({'width':'1000px'},1000);
});
});
But, let's assume that I'm not using jQuery and I want to call:
some_3secs_function(some_value);
some_5secs_function(some_value);
some_8secs_function(some_value);
How I should call this functions in order to execute some_3secs_function
, and AFTER that call ends, then execute some_5secs_function
and AFTER that call ends, then call some_8secs_function
?
UPDATE:
This still not working:
(function(callback){
$('#art1').animate({'width':'1000px'},1000);
callback();
})((function(callback2){
$('#art2').animate({'width':'1000px'},1000);
callback2();
})(function(){
$('#art3').animate({'width':'1000px'},1000);
}));
Three animations start at same time
Where is my mistake.
Since you tagged it with javascript, I would go with a timer control since your function names are 3, 5, and 8 seconds. So start your timer, 3 seconds in, call the first, 5 seconds in call the second, 8 seconds in call the third, then when it's done, stop the timer.
Normally in Javascript what you have is correct for the functions are running one after another, but since it looks like you're trying to do timed animation, a timer would be your best bet.
I use a 'waitUntil' function based on javascript's setTimeout
This would work perfectly if your functions set a certain condition to true, which you would be able to poll. Plus it comes with timeouts, which offers you alternatives in case your function failed to do something (even within time-range. Think about user feedback!)
eg
your functions should take a callback function, that gets called when it finishes.
then usage would be like:
You could also use promises in this way:
You would have to make
some_value
global in order to access it from inside the .thenAlternatively, from the outer function you could return the value the inner function would use, like so:
I won't go into a deep discussion of setTimeout here, but:
This answer uses
promises
, a JavaScript feature of theECMAScript 6
standard. If your target platform does not supportpromises
, polyfill it with PromiseJs.Look at my answer here Wait till a Function with animations is finished until running another Function if you want to use
jQuery
animations.Here is what your code would look like with
ES6 Promises
andjQuery animations
.Normal methods can also be wrapped in
Promises
.The
then
method is executed as soon as thePromise
finished. Normally, the return value of thefunction
passed tothen
is passed to the next one as result.But if a
Promise
is returned, the nextthen
function waits until thePromise
finished executing and receives the results of it (the value that is passed tofulfill
).