sleep 函数

  1. // Promise
  2. const sleep = time => new Promise(resolve => setTimeout(resolve, time));
  3. // ES5
  4. function sleep(callback, time) {
  5. setTimeout(callback, time);
  6. }
  1. async function test() {
  2. for (let index = 0; index < 10; index++) {
  3. console.log(index);
  4. await sleep(2000);
  5. }
  6. }
  7. test();