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()
posted @   橙云生  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
历史上的今天:
2020-05-13 webpack-2
点击右上角即可分享
微信分享提示