Push Functions into an Array - Loop through and Splice?

后端 未结 5 1300
南笙
南笙 2021-02-01 10:22

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 functio

5条回答
  •  春和景丽
    2021-02-01 11:15

    Is this what you needed?

    1: Push a certain amount of the same function (with a different parameter in each) into an array.

    function func(num){
        alert(num);
    }
    
    var k = [];
    k.push({f:func, params:[1]});
    k.push({f:func, params:[2]});
    k.push({f:func, params:[3]});
    

    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

    while (k.length > 0) {
     k[0].f(k[0].params);
     k.shift();
    }
    alert("done");
    

提交回复
热议问题