Javascript 在javascript中循环一个函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2281134/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:35:40  来源:igfitidea点击:

loop on a function in javascript?

javascript

提问by Sheery

Can we run a loop on a function in javascript, so that the function executes several times?

我们可以在 javascript 中对一个函数运行一个循环,以便该函数执行多次吗?

采纳答案by Jeeva Subburaj

if you tired of using standard like (IF, WHILE), here is the another way of doing.. :)

如果您厌倦了使用 (IF, WHILE) 之类的标准,这是另一种做法.. :)

you can use setTimeOut and clearTimeOut to execute functions in a certain interval. If you want to execute an function for a specific number of times, you still can acheive it by incrementing index and clearTimeOut as soon as your index reaches to certain point.

您可以使用 setTimeOut 和 clearTimeOut 在一定的时间间隔内执行函数。如果您想执行特定次数的函数,您仍然可以通过在索引到达某个点时立即增加 index 和 clearTimeOut 来实现它。

setTimeout() - executes a code some time in the future

clearTimeout() - cancels the setTimeout()

setTimeout() - 在未来某个时间执行代码

clearTimeout() - 取消 setTimeout()

example from w3schools

来自 w3schools 的例子

<html>
<head>
<script type="text/javascript">
var c=0;
var t;
var timer_is_on=0;

function timedCount()
{
document.getElementById('txt').value=c;
c=c+1;
t=setTimeout("timedCount()",1000);
}

function doTimer()
{
if (!timer_is_on)
  {
  timer_is_on=1;
  timedCount();
  }
}

function stopCount()
{
clearTimeout(t);
timer_is_on=0;
}
</script>
</head>

<body>
<form>
<input type="button" value="Start count!" onClick="doTimer()">
<input type="text" id="txt">
<input type="button" value="Stop count!" onClick="stopCount()">
</form>
</body>
</html>

回答by Josh Mein

Yes, you can use a forloop and a whileloop in javascript

是的,您可以在 javascript 中使用for循环和while循环

for (i=0;i<=5;i++)
{
    MyFunc();
}

Where the variable 'i' is the number of times it needs to run

其中变量“i”是它需要运行的次数

回答by Richard Friend

for (var i=0;i<10;i++) //Loop 10 times
{
//Do something
}

回答by Vincent Robert

Assuming your function is called f.

假设您的函数被调用f

function f()
{
    ...
}

Here is a Javascript loop that will run your function 10 times.

这是一个 Javascript 循环,它将运行您的函数 10 次。

for( int i = 0; i < 10; ++i )
{
    f();
}

回答by Skilldrick

Learn basic JavaScript at W3Schools. It's well worth the effort - it won't take long.

W3Schools学习基本的 JavaScript 。值得付出努力 - 不会花很长时间。

回答by Nikos M.

Here is another way to iterate (functionaly) over a function, that is also optimised (using loop unrolling), on average it will be up to 16times faster

这是在函数上迭代(功能性)的另一种方法,它也进行了优化(使用循环展开),平均而言它会16几倍

(adapted from a functional lib of mine use as you wish)

(根据您的意愿改编自我使用的功能库)

function iterate( F, i0, i1, F0 )
{
    if ( i0 > i1 ) return F0;
    else if ( i0 === i1 ) { F(i0, F0, i0, i1); return F0; }
    var l=i1-i0+1, i, k, r=l&15, q=r&1;
    if ( q ) F(i0, F0, i0, i1);
    for (i=q; i<r; i+=2)
    { 
        k = i0+i;
        F(  k, F0, i0, i1);
        F(++k, F0, i0, i1);
    }
    for (i=r; i<l; i+=16)
    {
        k = i0+i;
        F(  k, F0, i0, i1);
        F(++k, F0, i0, i1);
        F(++k, F0, i0, i1);
        F(++k, F0, i0, i1);
        F(++k, F0, i0, i1);
        F(++k, F0, i0, i1);
        F(++k, F0, i0, i1);
        F(++k, F0, i0, i1);
        F(++k, F0, i0, i1);
        F(++k, F0, i0, i1);
        F(++k, F0, i0, i1);
        F(++k, F0, i0, i1);
        F(++k, F0, i0, i1);
        F(++k, F0, i0, i1);
        F(++k, F0, i0, i1);
        F(++k, F0, i0, i1);
    }
    return F0;
}

use example:

使用示例:

function iterated(i, a, i0, i1)
{
     console.log([i, i0, i1]);
     a.push(i);
}
var a = iterate(iterated, 0, 9, []); // optionaly one can pass a parameter in the iterator
console.log(a);

output:

输出:

[0, 0, 9]
[1, 0, 9]
[2, 0, 9]
[3, 0, 9]
[4, 0, 9]
[5, 0, 9]
[6, 0, 9]
[7, 0, 9]
[8, 0, 9]
[9, 0, 9]

[0,1,2,3,4,5,6,7,8,9]