jQuery 迭代脚本 X 次
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6310206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iterate a script X times
提问by C_K
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.
我有几个功能可能需要也可能不需要重复一定次数。次数由用户设置,并存储在变量中:xTcount。
if (xTcount > 0) {
for (i=0; xTcount <= i; i++) {
$(document).miscfunction();
}
}
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”子句编码到每个可重复的函数中。如果可能的话,我想创建一些主检查器,简单地重复下一个调用的函数 xTcount 次......
回答by Matchu
Repeat some things xTcount
times? Maybe I'm misunderstanding, but it looks pretty simple:
重复一些事情xTcount
?也许我误解了,但它看起来很简单:
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:
如果问题是您不喜欢for
循环的外观,或者您的脚本要求您多次构建相同的循环,那么您可以在需要时提取它:
function repeat(fn, times) {
for(var i = 0; i < times; i++) fn();
}
repeat(doSomething, xTcount);
// ...later...
repeat(doSomethingElse, xTcount);