【发布时间】:2018-08-05 15:28:08
【问题描述】:
我有一系列不同的“不”。分钟',我想为他们一个接一个倒计时:
const steps = [{
label: "Label1",
time: 4
},
{
label: "Label2",
time: 2
},
{
label: "Label3",
time: 1
}
];
function countdownTimer(time) {
setTimeout(function () {
console.log("DONE");
}, parseInt(time + "000"));
}
function start() {
steps.forEach(function (step) {
countdownTimer(step.time);
});
}
但是,与 setTimeout 的性质一样,它们似乎都在同时运行,最短的时间首先显示。
如何让setTimeout依次运行,即显示4、2、1?
这是代码笔:
【问题讨论】:
-
在
countdownTimer(step.time, index)中传递每个计时器的索引并将其用作setTimeout的时间。
标签: javascript asynchronous foreach settimeout