Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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);
    }

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,762
    Thanks
    55
    Thanked 517 Times in 514 Posts
    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>

  • #3
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    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

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    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 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •