function sleep(time){
// 请写出你的代码
}
sleep(2000).then(()=>{
console.log("后续操作")
})
console.log(2);
目标 是让sleep 的功能与setTimeout一样:就是等2000毫秒之后再执行then中的回调函数
参考代码如下:
function sleep(time){
// 请写出你的代码
return new Promise(function(resolve,reject){
// 异步操作,根据执行结果,决定是否调用 resolve,reject
setTimeout(function(){
resolve()
}, time)
})
}
sleep(2000).then(()=>{
console.log("后续操作")
})
console.log(2);
本文介绍如何使用JavaScript编写一个名为sleep的函数,该函数的功能类似于setTimeout,可以让程序暂停指定的时间后执行后续操作。
2072

被折叠的 条评论
为什么被折叠?



