我怎么能使用javascript使它成为一个while循环在它的循环中间隔一秒钟?例如,如果我有
{nil=>\"\\n\\t\\t\\t\\t\\t\\t\", \"r12321\"=>\"Plains\", nil=>\" \", \"as124223\"=>\"Trains\", \"qwss12311232\"=>\"Automobiles\", nil=>\"\\n\\t\\t\\t\\t\\t\\t\", \"lknoijojkljl98799999\"=>\"Love\", nil=>\" \", \"vssdfsd0809809\"=>\"First\", \"awefsaf98098\"=>\"Sight\"}
我怎样才能使循环在if语句完成和while循环再次运行之间花费一秒钟?
答案 0 :(得分:0)
一个选项是使用setTimeout()
。
此示例允许在定时器之间切换
var timers = [{
name: "Timer 1",
count: 0
}, {
name: "Timer 2",
count: 0
}, {
name: "Timer 3",
count: 0
}];
var maxCount = 5; //change as per your need, 257
var allDone = function() {
var done = true;
var i = timers.length;
while (i--) {
if (maxCount > timers[i].count) {
done = false;
break;
}
}
return done;
};
var delay = 1000; //1 second
var loop = function() {
var timer = timers.shift(); // pick the first in queue
if (maxCount > timer.count)
timer.count++;
console.log(timer);
timers.push(timer); //put in the end of queue
if (!allDone())
setTimeout(loop, delay);
else
console.log('all done ...');
};
setTimeout(loop, delay);
Open console ..