tags:

views:

57

answers:

3

Hi. I am working on a project where I have a bunch of functions that I would like to call one by one in a particular order when a button is clicked. Once one function has been performed I do not want to use again I would like to move onto the next one.

It has been suggested by another S.O user that I need to use arrays but I am still learning jQuery and unsure how to do this, can anyone get me started in the right direction?

Many thanks in advance.

+2  A: 

Say you have functions like this:

function foo() { doSomeStuff; }
function bar() { doSomeOhterStuff; }

Or, an alternative but equal syntax:

var foo = function() { doSomeStuff; }
var bar = function() { doSomeOhterStuff; }

Then you can just create an array out of those function names and iterate them:

var functions = ['foo', 'bar'];

for(var i = 0; i <= functions.length; i++) {
    window[functions[i]]();
}

Or you can pass the functions directly to the array:

var functions = [foo, bar];

for(var i = 0; i <= functions.length; i++) {
    func();
}

The order you insert them to the array determines the order they are executed, and every function will be executed just once. Hope this helps.

Tatu Ulmanen
Yes it does thanks a lot for the help I will play around with this. One question is this jquery? I'm normally used to seeing $ somewhere before the function. I am learning so sorry if this is a stupid question!
mtwallet
I'll take it as a compliment that you added my example to the end of yours.
Hogan
No, this is just plain old JavaScript and does not require jQuery to work.
Tatu Ulmanen
@mtwallet: no this is just javascript, jquery is not needed.
Hogan
Thanks a lot for the help guys I will experiment with this. One last question I take it I can mix up Javascript and JQuery. I know Jquery IS Javascript but I wasn't sure about whether there would be conflicts like you can get with say Mootools and Jquery.
mtwallet
A: 
var inarr = [func1,func2,func3];

for (afunc in inarr) {
   afunc();

}

Hogan
I think, you have mistake in your code. First of all, it's a bad practice to loop through array with for...in. Second one, maybe you wanted to specify "inarr" instead of object ?
nemisj
oops, typo. thanks.
Hogan
+2  A: 

Example code :

function fnc1(){/* some code in here */};
function fnc2(){/* some code in here */};
function fnc3(){/* some code in here */};

var fncs = [ fnc1, fnc2, fnc3 ];
$("buttonId").click(function(){
    var currentFunction = (fncs.shift()); // removes first element from array
    if(currentFunction){
        currentFunction(); 
    }else{
        alert('No more functions available');
    }
});
nemisj
Will this call a single function per click or run all 4 on by one?
mtwallet
No, they will be called one by one. But, now i understand what you want, you want that, every time you click button, the different function is executed. I will change my answer, so that it meets your requirements :)
nemisj
@nemisj Thats great! i appreciate the help
mtwallet