如何实现sleep的效果(es5或者es6)
// 1. while循环 function sleep(ms){ var start = Date.now(), expire = start+ms; while( Date.now()<expire ); console.log('sleep结束...'); return; } // 2. Promise function sleep(ms){ var temple = new Promise( (resolve)=>{ setTimeout(resolve,ms); } ); return temple; } sleep(2000).then(function(){ console.log('sleep结束...'); }) // 3. async function sleep(ms){ return new Promise((resolve)=>setTimeout(resolve,ms)); } async function test(){ var temple = await sleep(1000); console.log('sleep结束...'); return temple; } test();