JavaScript 中如何优雅的实现 sleep ?
2022-11-15 20:40:19
sleep 函数的作用是使程序暂停指定的时间,起到延时的效果
```
async function sleep(time) {
return new Promise( (resolve) => {
setTimeout(resolve, time)
})
}
(async () => {
console.log(new Date())
await sleep(1000)
console.log(new Date())
await sleep(2000)
console.log(new Date())
await sleep(2000)
console.log(new Date())
})()
```