Open lgwebdream opened 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);
});
}
const sleep = (wait = 1000) => {
return new Promise(resolve => {
const timer = setTimeout(() => {
resolve();
clearTimeout(timer);
}, wait);
});
};
//这算sleep吗
let obj={
apple:2,
say: function(param) {
setTimeout(() => {
console.log(`等${param}`)
}, param * 1000)
return this
}
}
console.log(obj.say(2).apple)
扫描下方二维码,获取答案以及详细解析,同时可解锁800+道前端面试题。