Zac
Zac

Reputation: 12836

Fire functions consecutively with delay between each

I have a bunch of different functions in a class like this :

var helper = {    

     h1 : function(){
           //does  stuff        
    },

     h2 : function(){   
         //does  stuff
    },

    ...

}

and I can execute what I need with something like :

$('#helper').click(function(){
    helper.h4();
 // then delay then execute h5(), delay h6(), etc... 
     });

How could I have it so the functions execute consecutively with a defined delay between each. I am not sure how I could do this but I suspect I need to use queue, dequeue, and delay or something with setTimeout ?

Upvotes: 0

Views: 211

Answers (1)

RobG
RobG

Reputation: 147363

You could simply do:

setTimeout(helper.h4, 0);
setTimeout(helper.h5, 1000);
setTimeout(helper.h6, 2000);
…

Note that each timeout will be started immediately, so the delay is from the same instant (more or less). You might want to capture the returned values from the calls so you can cancel timeouts that haven't been called yet for whatever reason.

Upvotes: 1

Related Questions