3

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.

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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!
I'll take it as a compliment that you added my example to the end of yours.
No, this is just plain old JavaScript and does not require jQuery to work.
@mtwallet: no this is just javascript, jquery is not needed.
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.
2

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');
    }
});

4 Comments

Will this call a single function per click or run all 4 on by one?
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 :)
Thats great! i appreciate the help
Would love to see @nemisj's original answer here, as it solves my problem.
0
var inarr = [func1,func2,func3];

for (afunc in inarr) {
   afunc();
}

1 Comment

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 ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.