ForeveHG / Frontend-Daily-Interview

学习,尝试回答一些前端面试题
1 stars 0 forks source link

23. 实现一个sleep函数 #23

Open ForeveHG opened 4 years ago

ForeveHG commented 4 years ago

比如sleep(1000)意味着等待1000毫秒,可以从Promise,Generator,Async/await等角度

ForeveHG commented 4 years ago
  1. 循环阻塞主线程,真正意义上的sleep

  2. 定时器

  3. Promise+定时器

    
    function sleep(delay) {
    return new Promise(resolve => {
    setTimeout(resolve,delay)
    })
    }
  4. async + Promise + 定时器