jQuery数组/从数组按顺序调用函数 [英] jquery array/call function from array in order
本文介绍了jQuery数组/从数组按顺序调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在一个项目中,我有很多函数,当单击一个按钮时,我想按特定的顺序逐个调用这些函数.一旦执行了一项功能,我就不想再使用了.我想转到下一项功能.
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.
另一位S.O用户建议我需要使用数组,但是我仍在学习jQuery,不确定如何执行此操作,有人可以引导我朝着正确的方向迈进吗?
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?
非常感谢.
推荐答案
示例代码
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');
}
});
这篇关于jQuery数组/从数组按顺序调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文