T
T
TakoY2013-12-24 01:54:06
JavaScript
TakoY, 2013-12-24 01:54:06

How to execute functions in a queue that contains timers and animations?

It is necessary to go through an array of functions in a loop and execute them so that each next one starts after the timers and animations of the previous one are completely completed
Unfortunately, I could not figure out this task

<button id="d1">111</button>
<button id="d2">222</button>
<button id="d3">333</button>
          
<script  type="text/javascript">

function d1() {
    $('#d1').slideUp(1000).slideDown(1000);
}   
         
function d2() {
    setTimeout(function(){
        $('#d2').hide(1000).show(1000);
    },2000);
}   
function d3() {
    $('#d3').fadeOut(1000).fadeIn(1000);
}  
  
var arf=[d1, d2, d3];  
    
for(var i = 0; i<arf.length; i++) {   
    // .....
}

</script>

Answer the question

In order to leave comments, you need to log in

6 answer(s)
T
trushka, 2013-12-24
@trushka

maybe so?

function d1(f) {
    $('#d1').slideUp(1000).slideDown(1000, f);
}   
         
function d2(f) {
    setTimeout(function(){
        $('#d2').hide(1000).show(1000, f);
    },2000);
}   
function d3(f) {
    $('#d3').fadeOut(1000).fadeIn(1000, f);
}  
  
var arf=[d1, d2, d3];  
    
for(var i = 0; i<arf.length; i++) { 
    arf[i](arf[i+1])
    // .....
}

L
lnked, 2013-12-24
@lnked

maybe so

function d1() {
    $('#d1').slideUp(1000).slideDown(1000);
    cb();
}
         
function d2() {
    setTimeout(function(){
        $('#d2').hide(1000).show(1000);
        cb();
    },2000);
}

function d3() {
    $('#d3').fadeOut(1000).fadeIn(1000);
    cb();
}

var fns = [ d1, d2, d3 ];

function cb()
{
    var fn = fns.shift();
    if( typeof fn == 'function' ) fn.call();
}

cb();

D
DrNemo, 2013-12-24
@DrNemo

I'm afraid only callback will help here

A
Alexander, 2013-12-24
@asx

Rather Deferred.
And it is well written about him here - anton.shevchuk.name/jquery-book (from page 83)

A
Alexey Zakharov, 2013-12-24
@Zakhar0v

not strong in js, but why not return some value to the function on execution, and then through

switch (returnedValue) {
    //...
}

X
xmeoff, 2013-12-24
@xmeoff

What you need is in this article .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question