Iterate a script X times

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

if (xTcount > 0) {
    for (i=0; xTcount <= i; i++) {
        $(document).miscfunction();
    }

}

I really have not tested the script above since I am sure that this is not true. What makes what I want more complicated is that I don’t want to encode the "check xTcount" clause into every function that repeats. if possible, I would like to create a master controller that simply repeats the following function xTcount times ...

+3
source share
1 answer

Repeat a few things xTcountonce? I may be misunderstood, but it looks pretty simple:

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

, for script , , , :

function repeat(fn, times) {
    for(var i = 0; i < times; i++) fn();
}

repeat(doSomething, xTcount);
// ...later...
repeat(doSomethingElse, xTcount);
+14

All Articles