Promise

  1. function sleep(time = 1) {
  2. return new Promise((resolve, reject) => {
  3. setTimeout(() => {
  4. resolve(true)
  5. }, time);
  6. })
  7. }

while循环

  1. function sleep(time = 1) {
  2. let start = +Date.now()
  3. while(true) {
  4. if (+Date.now() - start >= time) break
  5. }
  6. }

for循环

  1. function sleep(time = 1) {
  2. for(let t = Date.now(); Date.now() - t <= time;) {}
  3. }