...

View Full Version : Javascript - Push Functions into an Array - Loop through and Splice?



anthony_reynold
09-24-2012, 09:51 PM
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);
}

xelawho
09-24-2012, 10:42 PM
I understand that your example is much simplified from what you are trying to do, but wouldn't it be better to store an array of parameters and loop through them, maintaining just one function?



<script>
arr=[2,4,6,9]
for(var i=arr.length-1; i>-1; i--) {
myfunc(arr[i]);
}

function myfunc(num){
if (arr.length == 1) {
alert("done");
} else {
alert(num)
arr.pop()
}
}

alert(arr)
</script>

Logic Ali
09-24-2012, 11:33 PM
<script type="text/javascript">

array = [];

array.push( function(){ func(1) } );
array.push( function(){ func(2) } );
array.push( function(){ func(3) } );

while ( arrayManager() )
;

alert( 'Done' );

function arrayManager()
{
if(array.length > 0)
array.splice( 0, 1 )[0]();

return array.length;
}

function func(num)
{
alert(num);
}

</script>
In future don't dump the same question into every forum you can find; yes we know why you did it but people don't like it.

rnd me
09-25-2012, 08:38 PM
you can use [].pop() to take on from the top or [].shift() to grab one from the bottom.

to delete, splice takes 2 arguments, the first of which is the slot # to delete, and the second one is the # of slots to delete. (0,1) deletes the first one, (-1,1) deletes the last one, (0, ar.length) deletes them all, and (5,5) deletes # 5-10.

if that's too complicated, just set the used array indexes to null and call ar=ar.filter(Boolean) to strip out any slots you nulled.


but your example won't work as shown, your array will be empty because you are storing the result of calling func, which is nothing...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum