[英]Timed for loop in Javascript
I don't even know how to get started with this: I need a for loop that executes a function
(say a simple console.log()
) with a timed delay between each execution. 我什至不知道如何开始:我需要一个for循环来执行一个function
(例如,一个简单的console.log()
),每次执行之间都有一定的延时。 I've been trying to do it with setTimeout()
and it never works. 我一直在尝试使用setTimeout()
,但它永远无法正常工作。 If I call the function that has the loop from setTimeout, it won't work. 如果我从setTimeout调用具有循环的函数,它将无法正常工作。 Ideally I'd want my for
loop to print something x times, with a couple of seconds delay between each printing. 理想情况下,我希望for
循环可以打印x次,每次打印之间要延迟几秒钟。 Any ideas how that might work? 任何想法可能如何工作? I've tried something like this: 我已经尝试过这样的事情:
function printStuff(){
for(var i=0;i<5;i++){
console.log(i);
}
};
setTimeout(printStuff(),1000);
For me you should execute setInterval
and inside this you should increase counter. 对我来说,您应该执行setInterval
,在其中应该增加计数器。 When counter reach the limit you simply clear interval. 当计数器达到极限时,您只需清除间隔。
var counter = 0;
var limit = 10;
var myVar = setInterval(function(){
if (counter > limit)
{
clearInterval(myVar);
}
counter++;
console.log("test");
}, 1000);
init();
function init() {
setTimeout(init, 2*1000); // wait 2 sec then call init again
console.log(Date());
}
Or use setInterval
: 或使用setInterval
:
// Call init after 2 sec and repeat calling it every 2. sec
setInterval(init, 2*1000);
function init() {
console.log(Date());
}
var i = 0;
function timeout(){
setTimeout(log, 1000);
}
function log(){
console.log(i++);
timeout();
}
log();
Use setInterval()
instead of setTimeout()
. 使用setInterval()
而不是setTimeout()
。 Parameters are just the same: 参数是相同的:
setInterval(function () {
// your utility code goes here
}, 2000);
Here is one more way to do it. 这是另一种方法。 Use a wrapper function. 使用包装函数。
var time = 2000;
for (var i = 0; i < 10; i++) {
(function (i) {
setTimeout(function () {
console.log(i);
}, time);
})(i);
time+=2000;
}
You can create a sort of delayed loop function with the number of iterations/times you want to run. 您可以使用要运行的迭代次数/次数创建一种延迟循环函数。 Something like this: 像这样:
var delayedLoop = function (n, milliseconds) {
var iteration = function (n) {
if (n > 0) {
n--;
console.log(n);
setTimeout(function () {
iteration(n)
}, milliseconds);
}
};
iteration(n);
}
delayedLoop(4, 1000);
You could even expand the idea and even passing a function to be executed each time. 您甚至可以扩展想法,甚至传递每次要执行的功能。
Here's what I think is simpler ( and doesn't have the fallbacks of ) than a setInterval 这是我认为比setInterval更简单( 并且没有fallback的 )
var limit = 10, counter = 0, delay = 1000; function doIt() { document.body.innerHTML += 'Hit counter: ' + (counter++) + '<br />'; if (counter < limit) { setTimeout(doIt, delay); } } doIt();
And you can generalize it 你可以概括一下
function runTimedLoop(delay, howMany, callback) { var index = 0; function iteration() { callback(index++); if (index < howMany) { setTimeout(iteration, delay); } } iteration(); } runTimedLoop(1000, 10, function(index) { document.body.innerHTML += 'Hit counter: ' + (index++) + '<br />'; });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.