lgwebdream / FE-Interview

🔥🔥🔥 前端面试,独有前端面试题详解,前端面试刷题必备,1000+前端面试真题,Html、Css、JavaScript、Vue、React、Node、TypeScript、Webpack、算法、网络与安全、浏览器
https://lgwebdream.github.io/FE-Interview/
Other
6.82k stars 896 forks source link

手写实现 sleep 函数 #267

Open lgwebdream opened 4 years ago

lgwebdream commented 4 years ago

扫描下方二维码,获取答案以及详细解析,同时可解锁800+道前端面试题。

GolderBrother commented 4 years ago
function sleep(fn, delay, ...args) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      try {
        const result = typeof fn === 'function' && fn.apply(this, args);
        resolve(result);
      } catch (error) {
        reject(error);
      }
    }, delay);
  });
}
gaohan1994 commented 5 months ago
const sleep = (wait = 1000) => {
  return new Promise(resolve => {
    const timer = setTimeout(() => {
      resolve();
      clearTimeout(timer);
    }, wait);
  });
};
image
Alicca-miao commented 5 days ago
//这算sleep吗
let obj={
    apple:2,
    say: function(param) {
        setTimeout(() => {
            console.log(`等${param}`)
        }, param * 1000)
        return this
    }
}

  console.log(obj.say(2).apple)