将函数推入数组 - 循环通过和拼接? [英] Push Functions into an Array - Loop through and Splice?
问题描述
使用Javascript我需要能够:
1:将一定数量的相同功能(每个参数不同)推入一个数组。 / p>
2:然后逐个运行每个函数(本例中只是参数/数字的警告)
3:在每个函数之后我需要能够从数组中SPLICE函数
4:每次检查数组长度 - 一旦数组再次为空 - 警告用户它是完整的
现在我似乎能够完成任务1,2和4,但我在如何从数组中拼出该函数后磕磕绊绊它已经运行 - 有人可以帮忙吗?因为我无法删除该功能所以在调用所有函数后我永远不会收到完成警告
到目前为止我的javascript代码是:
//创建空数组
var array = [];
//将函数推入数组 - 动态数量,可以是任意数量的函数
array.push(func(1));
array.push(func(2));
array.push(func(3));
//推送数组后调用数组管理器函数
arrayManager();
//完成时拼接和检测的数组管理器函数
函数arrayManager(){
if(array.length< 1){
alert(done );
}
else {
//////////////////////////////////
//<<这就是我不知道如何从阵列中剔除物品
////////////////////////////// ////
}
}
//数组对象的函数 - 警告传递参数
函数func(num){
alert(num) ;
}
首先你不是此时将函数推入数组,您可以执行func。要实现推送,你的func应如下所示:
//数组对象的函数 - 警告传递参数
函数func(num){
return function(){
alert(num);
}
}
现在如果你的函数是同步的,你可以简单地迭代数组
for(var i in arr){
arr [i]();
}
console.log('done');
如果我们正在处理异步函数,那么他们需要进行回调:
//数组对象的函数 - 警告传递参数
函数func(num){
返回函数(回调){
alert(num);
callback();
}
}
然后你可以使用一个计数器来运行并行。
var count = arr.length;
for(var i in arr){
arr [i](function(){
if( - count === 0){
console.log('Done ');
}
});
}
或按顺序:
function run(){
var fn = arr.shift();
if(!fn){
console.log('Done');
} else {
fn(run);
}
}
run();
Using Javascript i need to be able to:
1: Push a certain amount of the same function (with a different parameter in each) into an array.
2: Then run each function one by one (for this example just an alert of the parameter/number)
3: After each function i need to be able to SPLICE that function out of the array
4: Check the Array Length after everytime - Once the array is empty again - alert the user it is complete
Now i seem to be able to do task 1,2 and 4 but i am sturggling with how to splice out the function from the array after it has run - can anyone help? As i cannot remove the function i am never getting the 'done' alert once all functions have been called
My javascript code so far is:
// Create empty array
var array = [];
// Push functions into array - dynamic amount and could be any amount of functions
array.push(func(1));
array.push(func(2));
array.push(func(3));
// Call array manager function after pushing array
arrayManager();
// Array manager function to splice and detect when finished
function arrayManager() {
if (array.length < 1) {
alert("done");
}
else {
//////////////////////////////////
// << THIS IS WHERE I DON'T KNOW HOW TO SPLICE THE ITEM FROM THE ARRAY
//////////////////////////////////
}
}
// Function for array objects - alert passed parameter
function func(num){
alert(num);
}
First of all you are not pushing functions into the array at the moment, you execute the func instead. To achieve the push your func should look like this:
// Function for array objects - alert passed parameter
function func(num){
return function(){
alert(num);
}
}
Now if your functions are synchronous you could simply iterate over the array
for(var i in arr){
arr[i]();
}
console.log('done');
If we are dealing with asynchronous functions then they need to have a callback:
// Function for array objects - alert passed parameter
function func(num){
return function(callback){
alert(num);
callback();
}
}
And then you can either use a counter to run in parallel.
var count = arr.length;
for(var i in arr){
arr[i](function(){
if(--count === 0){
console.log('Done');
}
});
}
Or in sequence:
function run(){
var fn = arr.shift();
if(!fn){
console.log('Done');
} else {
fn(run);
}
}
run();
这篇关于将函数推入数组 - 循环通过和拼接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!