vote up 2 vote down star

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.

flag

3 Answers

vote up 2 vote down check

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');
    }
});
link|flag
Will this call a single function per click or run all 4 on by one? – mtwallet Feb 5 at 14:45
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 Feb 5 at 14:46
@nemisj Thats great! i appreciate the help – mtwallet Feb 5 at 14:54
vote up 2 vote down

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.

link|flag
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 Feb 5 at 14:41
I'll take it as a compliment that you added my example to the end of yours. – Hogan Feb 5 at 14:43
No, this is just plain old JavaScript and does not require jQuery to work. – Tatu Ulmanen Feb 5 at 14:43
@mtwallet: no this is just javascript, jquery is not needed. – Hogan Feb 5 at 14:43
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 Feb 5 at 14:49
vote up 0 vote down
var inarr = [func1,func2,func3];

for (afunc in inarr) {
   afunc();

}

link|flag
1  
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 Feb 5 at 14:40
oops, typo. thanks. – Hogan Feb 5 at 14:41

Your Answer

Get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.