setTimeout模拟interval
function runTimer(list = [
{
delay:2000,
text:'第一步延迟2s'
},
{
delay:3000,
text:'第二步延迟3s'
},
{
delay:1000,
text:'第三步延迟1s'
},
], cb = (text)=>{
console.log('渲染回调',text)
}){
let i = 0;
let t;
function run(){
const task = list[i];
clearTimeout(t);
if(!task){
console.log('onEnd')
return
}
console.time();
cb(task.text)
t = setTimeout(() => {
clearTimeout(t);
i += 1;
console.timeEnd();
run(i)
},task.delay);
}
run()
}
runTimer()