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 @ 2024-05-13 16:28  橙云生  阅读(5)  评论(0编辑  收藏  举报