【发布时间】:2014-02-08 16:03:31
【问题描述】:
如何将一个函数调用10次
for(x=0; x<10; x++) callfunction();
但每次通话之间有 1 秒?
【问题讨论】:
标签: javascript timing function-call
如何将一个函数调用10次
for(x=0; x<10; x++) callfunction();
但每次通话之间有 1 秒?
【问题讨论】:
标签: javascript timing function-call
function callNTimes(func, num, delay) {
if (!num) return;
func();
setTimeout(function() { callNTimes(func, num - 1, delay); }, delay);
}
callNTimes(callfunction, 10, 1000);
编辑:该函数基本上是说:调用传递的函数,然后再执行 9 次。
【讨论】:
您可以使用 setInterval 以间隔重复执行,然后在 10 次调用后使用 clearInterval:
callfunction();
var callCount = 1;
var repeater = setInterval(function () {
if (callCount < 10) {
callfunction();
callCount += 1;
} else {
clearInterval(repeater);
}
}, 1000);
补充:但是如果你不知道你的 callfunction 需要多长时间执行并且调用起始点之间的准确时间并不重要,那么由于 Paul 提到的原因,似乎最好使用 setTimeout S 和this article 中描述的那些。
【讨论】:
setInterval,尤其是当您不知道callfunction 需要多长时间才能完成调用时(如果代码在不到 1 秒的时间内重复使用则更重要间隔),因为您最终可能会遇到讨厌的..级联?
另一种解决方案
for(var x=0; x<10; x++) window.setTimeout(callfunction, 1000 * x);
【讨论】:
您可以尝试使用setInterval 并使用变量数到10。试试这个:
var number = 1;
function oneSecond () {
if(number <= 10) {
// execute code here..
number++;
}
};
现在使用 setInterval:
setInterval(oneSecond, 1000);
【讨论】:
var number == '1'; 不会创建变量。 == 用于比较。
类似于Amadan's answer,但闭包风格不同,这意味着您可以重复使用而不是创建新函数
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
在本例中,如果您将times 设置为0 或Infinity,它将永远运行。
【讨论】:
我不知道有没有合适的名字,但我用的是中继器:
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);
}
然后你可以像这样使用它:
new Repeater(callfunction, 1000, 10); // 1 second delay, 10 times
【讨论】:
setTimeout() 调用run() 时,this 值将不会指向Repeater 对象。您必须使用闭包或.bind() 来修复。
var rpt = new Repeater(function() { doSomethingWith(rpt); }, delay, conut);
setTimeout() 时,setTimeout() 保存的所有内容都是对 run() 的函数引用,并且没有使用正确的 this 值调用它,因此您的代码将无法工作。
self.run()而不是self.run.call(this);。
const functionCounterTimer = (callCount) => {
if (callCount < 10) {
setTimeout(() => {
++callCount
console.log("Function Call ", callCount);
functionCounterTimer(callCount);
}, 1000);
}
}
functionCounterTimer(0);
以上是我对类似问题的处理方法。
【讨论】:
setInterval(function(){},1000);
每秒调用一次函数...
你也可以使用 setTimeout 来让你的东西工作。
【讨论】: