javascript javascript调用函数10次,间隔1秒

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

javascript Call function 10 times with 1 second between

javascripttimingfunction-call

提问by user3129452

How to call a function 10 times like

如何调用一个函数 10 次

for(x=0; x<10; x++) callfunction();

but with 1 sec between each call?

但每次通话间隔 1 秒?

回答by Amadan

function callNTimes(func, num, delay) {
    if (!num) return;
    func();
    setTimeout(function() { callNTimes(func, num - 1, delay); }, delay);
}
callNTimes(callfunction, 10, 1000);

EDIT: The function basically says: make a call of the passed function, then after a bit, do it again 9 more times.

编辑:该函数基本上是说:调用传递的函数,然后再调用 9 次。

回答by aemxdp

You can use setIntervalfor repeated execution with intervals and then clearIntervalafter 10 invocations:

您可以使用setInterval以间隔重复执行,然后在 10 次调用后使用clearInterval

callfunction();
var callCount = 1;
var repeater = setInterval(function () {
  if (callCount < 10) {
    callfunction();
    callCount += 1;
  } else {
    clearInterval(repeater);
  }
}, 1000);

Added:But if you don't know how long it takes your callfunction to execute and the accurate timings between invocation starting points are not important it seems it's better to use setTimeout for reasons mentioned by Paul S and those described in this article.

补充:但是如果您不知道执行调用函数需要多长时间,并且调用起点之间的准确计时并不重要,那么出于 Paul S 提到的原因和本文中描述的原因,似乎最好使用 setTimeout 。

回答by HenningCash

Another solution

另一种解决方案

for(var x=0; x<10; x++) window.setTimeout(callfunction, 1000 * x);

回答by Afzaal Ahmad Zeeshan

You can try to use setIntervaland use a variable to count up to 10. Try this:

您可以尝试使用setInterval并使用一个变量来数到 10。试试这个:

var number = 1;
function oneSecond () {
  if(number <= 10) {
    // execute code here..
    number++;
  }
};

Now use the setInterval:

现在使用 setInterval:

setInterval(oneSecond, 1000);

回答by Niet the Dark Absol

I don't know if there's a proper name, but I use a repeater:

我不知道是否有正确的名称,但我使用中继器:

function Repeater(callback, delay, count) {
    var self = this;
    this.timer = setTimeout(function() {self.run();},delay);
    this.callback = callback;
    this.delay = delay;
    this.timesLeft = count;
    this.lastCalled = new Date().getTime();
}
Repeater.prototype.run = function() {
    var self = this;
    this.timesLeft--;
    this.callback();
    this.lastCalled = new Date().getTime();
    if( this.timesLeft > 0) {
        this.timer = setTimeout(function() {self.run();},this.delay);
    }
}
Repeater.prototype.changeDelay = function(newdelay) {
    var self = this;
    clearTimeout(this.timer);
    this.timer = setTimeout(function() {self.run();},
                          newdelay-new Date().getTime()+lastcalled);
    this.delay = newdelay;
}
Repeater.prototype.changeCount = function(newcount) {
    var self = this;
    if( this.timesLeft == 0) {
        this.timer = setTimeout(function() {self.run();},this.delay);
    }
    this.timesLeft = newcount;
    if( this.timesLeft == 0) clearTimeout(this.timer);
}

You can then use it like this:

然后你可以像这样使用它:

new Repeater(callfunction, 1000, 10); // 1 second delay, 10 times

回答by Paul S.

Similar to Amadan's answerbut with a different style of closure which means you re-use instead of creating new functions

类似于Amadan 的答案,但具有不同的闭包风格,这意味着您可以重用而不是创建新功能

function call(fn, /* ms */ every, /* int */ times) {
    var repeater = function () {
        fn();
        if (--times) window.setTimeout(repeater, every);
    };
    repeater(); // start loop
}
// use it
var i = 0;
call(function () {console.log(++i);}, 1e3, 10); // 1e3 is 1 second
// 1 to 10 gets logged over 10 seconds

In this example, if you were to set timesto either 0or Infinity, it would run forever.

在本例中,如果您设置times0Infinity,它将永远运行。

回答by Karthik Surianarayanan

setInterval(function(){},1000);

Calls the function for every second...

每秒调用一次函数...

You can also use setTimeout for your thing to work.

您还可以使用 setTimeout 来让您的工作正常工作。