English 中文(简体)
阵列/召唤功能
原标题:jquery array/call function from array in order

我正在实施一项项目,在被点击时,我会按某个具体顺序把一个职能推给一个。 一旦履行一项职能,我不想再次使用,我就希望下一步工作。

另一位S.O用户建议,我需要使用阵列,但我仍在学习笑,不敢这样做,任何人能否从正确方向开始?

很多人预先表示感谢。

最佳回答

例:

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 );
    }
});
问题回答

你有这样的职能:

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

或者,一种替代但平等的合成物:

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

然后,你就能够创造出这些职务名称的阵列,并为之注入活力:

var functions = [ foo ,  bar ];

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

或者你可以直接将职能传递给阵列:

var functions = [foo, bar];

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

插入这些表格的顺序决定了执行命令的顺序,每个职能将只执行一次。 希望这一帮助。

var inarr = [func1,func2,func3];

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




相关问题
WordPress Data Storage Efficiency

I ve been asked to review a WordPress plugin of sorts and try to find ways of making it faster. The premise of this plugin is basically to store a bunch of users and shifts and appointments and ...

Convert a 2D array index into a 1D index

I have two arrays for a chess variant I am coding in java...I have a console version so far which represents the board as a 1D array (size is 32) but I am working on making a GUI for it and I want it ...

Convert an array of integers for use in a SQL "IN" clause

Surely there is a framework method that given an array of integers, strings etc converts them into a list that can be used in a SQL "IN" clause? e.g. int[] values = {1,2,3}; would go to "(1,2,3)"

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

C++ Array Sort Me

Stuck on an array sorter. Have to sort numbers from largest to smallest. I m trying two loops (one nested in the other). Here s the code: int counter=0; // inner counter int counter2=0; // outer ...

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

Best practice of big javascript objects

sry for this imprecise topic name. I am querying a dataset a lot of times so using ajax request would end up in tons of http requests. For this reason I decided to use the json encode method to ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

热门标签