Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-24-2012, 08:51 PM   PM User | #1
anthony_reynold
New to the CF scene

 
Join Date: Sep 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
anthony_reynold is an unknown quantity at this point
Javascript - Push Functions into an Array - Loop through and Splice?

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:

Code:
// 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);
}
anthony_reynold is offline   Reply With Quote
Old 09-24-2012, 09:42 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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?

Code:
<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>
xelawho is offline   Reply With Quote
Old 09-24-2012, 10:33 PM   PM User | #3
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 959
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Code:
<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.

Last edited by Logic Ali; 09-24-2012 at 10:42 PM.. Reason: Narked
Logic Ali is offline   Reply With Quote
Old 09-25-2012, 07:38 PM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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...
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%
rnd me is offline   Reply With Quote
Reply

Bookmarks

Tags
arrays, javascript, push, splice

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:18 AM.


Advertisement
Log in to turn off these ads.