问题描述
我有几个功能可能需要或可能不需要重复一定次数。次数由用户设置,并存储在变量:xTcount中。 (xTcount> 0){
for(i = 0; xTcount< = i; i ++)
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.
if (xTcount > 0) {
for (i=0; xTcount <= i; i++) {
$(document).miscfunction();
}
}
我没有真正测试过上面的脚本,因为我敢肯定这是不正确的。什么使我想要的技巧变得棘手,就是我不想为每个可重复的函数编写一个check xTcount子句。如果可能的话,我想创建一些主检查器,只需重复下一次调用的函数xTcount次... ...
I haven't actually tested the script above, as i'm sure it's incorrect. What makes what i want tricky, is that i don't want to have to code a "check xTcount" clause into every function that is repeatable. if possible, i'd like to create some master checker that simply repeats the next-called function xTcount times...
推荐答案
重复一些事情 xTcount
次?也许我误解了,但看起来很简单:
Repeat some things xTcount
times? Maybe I'm misunderstanding, but it looks pretty simple:
for(var i = 0; i < xTcount; i++) {
doSomething();
doSomethingElse();
}
如果麻烦的是你不喜欢 for
循环,或者你的脚本要求你多次构建相同的循环,如果你愿意的话可以提取它:
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);
这篇关于迭代脚本X次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!