In jQuery you can chain actions for the same selector very easily but if you want to use different selector for each action it requires nested $.when
with more then two actions which is quite hard to read and maintain.
HTML:
<span id='a'>Hello</span>
<span id='b'>world</span>
<span id='c'>!!!</span>
CSS:
span {
display: none;
}
JS: based on this: how to hide multiple items but only call the handler once?
var d = 500; // duration
// Execute in parallel.
//$('#a').show(d).hide(d);
//$('#b').show(d).hide(d);
//$('#c').show(d).hide(d);
$.when($('#a').fadeIn(d).fadeOut(d)).done(function () {
$.when($('#b').show(d).hide(d)).done(function () {
$('#c').slideDown(d).slideUp(d);
});
});
jsfiddle (old)
jsfiddle-2
I thougt I could use the queue but it seems to work only for the same selector.
Is there a way to write it in a more maintainable manner like:
pseudocode
var myActions = [];
myActions.push(function(){...});
myActions.push(function(){...});
myActions.push(function(){...});
something.executeSequentially(myActions);
EDIT:
I updated the demo so that it's a little bit harder.
If you really don't have to encounter for failures (and that's hardly possible with animations I suppose), you can use the following approach (kudos to @Esailija, as this solution is basically a simplified version of his answer):
var chainOfActions = [
function() { return $('#a').fadeIn(d).fadeOut(d); },
function() { return $('#b').fadeIn(d).fadeOut(d); },
function() { return $('#c').fadeIn(d).fadeOut(d); },
];
chainOfActions.reduce(function(curr, next) {
return curr.then(next);
}, $().promise());
Demo. There are three key points here:
.promise()
call instead)reduce
a chain is created, as each callback supplied into then()
creates a new promisereduce
accumulator.Edit, Updated
var d = 500
, something = {}
, myActions = [];
myActions.push(
function (next) {
$('#a').fadeIn(d).fadeOut(d, next)
});
myActions.push(
function (next) {
return $('#b').show(d).hide(d, next)
});
myActions.push(
function () {
return $('#c').slideDown(d).slideUp(d)
});
something.executeSequentially = function (arr) {
return $(this).queue("fx", arr);
};
something.executeSequentially(myActions);
jsfiddle http://jsfiddle.net/guest271314/2oawa1zn/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With