实现sleep函数
1.使用async,await
function sleep(time){ return new Promise((resolve,reject)=>{ setTimeout(resolve,time) }) } async function init(){ console.log('do something') await sleep(3000) console.log('睡完之后 do something') }
2.
function sleep(callback,time){ setTimeout(callback,time) } sleep(()=>{ console.log('睡一段时间后,我就执行了') },5000)