Iterate a script X times

后端 未结 1 1971
逝去的感伤
逝去的感伤 2021-01-19 22:00

I have several functions that may or may not need to be repeated a certain number of times. The number of times is set by the user, and is stored in a variable: xTcount.

1条回答
  •  野的像风
    2021-01-19 22:32

    Repeat some things xTcount times? Maybe I'm misunderstanding, but it looks pretty simple:

    for(var i = 0; i < xTcount; i++) {
        doSomething();
        doSomethingElse();
    }
    

    If the trouble is that you don't like the look of the for loop, or that your script requires you to build the same loop multiple times, you could extract it if you reeeaaallllly wanted to:

    function repeat(fn, times) {
        for(var i = 0; i < times; i++) fn();
    }
    
    repeat(doSomething, xTcount);
    // ...later...
    repeat(doSomethingElse, xTcount);
    

    0 讨论(0)
提交回复
热议问题