Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute an array of functions

I'm using a mixture of d3.js and jQuery to create a visualisation. I have 3 functions that I'm trying to put into an array and then execute one after the other but I don't think I'm doing it correctly as when I click "play" nothing happens. Here's my code:

var functionsArray = [oct12,oct13,oct14];

$('#play').click(function(){ 
for (var i = 0; i < functionsArray.length; i++){
    functionsArray[i];  
}

I'll put up a jsfiddle shortly...

like image 819
zik Avatar asked Feb 19 '23 19:02

zik


2 Answers

You need to call the function too.

functionsArray[i]();  
like image 142
Dogbert Avatar answered Feb 24 '23 00:02

Dogbert


use $.each

demo

var functionsArray = [oct12,oct13,oct14];

$(functionsArray).each(function(key, val){
 val();
});

function oct12(){
 alert('oct12');
}

function oct13(){
 alert('oct13');
}

function oct14(){
  alert('oct14');
}

like image 39
Pragnesh Chauhan Avatar answered Feb 23 '23 23:02

Pragnesh Chauhan